Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Thursday, April 16, 2009

欲在Symphony(PDO使用Propel)下從DB拿多列的資料,目前看到這是唯一可行的方法:



$connection = Propel::getConnection();
$query = 'SELECT MAX(%s) AS max FROM %s'; //写你要查询的sql
$query = sprintf($query, ArticlePeer::CREATED_AT, ArticlePeer::TABLE_NAME);
$statement = $connection->prepare($query);
$statement->execute();
$resultset = $statement->fetchAll(PDO::FETCH_OBJ);

最後一行如果用fetch就是只拿一列,fetchAll就拿所有列。 $resultset是個array,裡面有所有列的資料。

但這是Propel裡的東西,而非Symfony本身的。 Symfony本身的doSelect()只能拿到第一筆的資料,但列的數目又是對的。 我查了好久的文件還是找不到為什麼會這樣。 doSelectstmt()也懶得試了… Symfony真的很鳥啊!

Edit:
用了doSelect

$c = new Criteria();
$c->add(ListsPeer::MAC_ADDRESS, $mac, Criteria::EQUAL);
$stmt = ListsPeer::doSelectStmt($c);
$ids = array();
while($res = $stmt->fetchColumn(1)) {
echo $res;
}

還是可以用的啦… 算了。

Tuesday, April 14, 2009

Using Propel ORM. My step-by-step walkthrough to create a working model:

There are several commands concerning the ORM which ultimately works toward building a Model.

The schema.yml, which represents the schema for a Model, can be either typed by hand, or imported and built directly from a database. To do so, type:


symfony propel:build-schema


Or if there is no corresponding database in the db, you may enter the schema by hand. After doing so, type


symfony propel:build-sql


to convert the schema into SQL commands. The actual SQL commands are stored in the lib.model.schema.sql inside the data/sql/ directory of the project folder. You can actually skip the build-sql step by editting this file directly...

After the build-sql command, you can insert the SQL commands into the db, provided that you have already configured the connection to your db. Use either


symfony configure:database "mysql:host=localhost;dbname=db_name"


or edit your /config/databases.yml and /config/propel.ini

Once the connection to the db server are set, insert the SQL by typing:


symfony propel:insert-sql


This step will remove tables with the same names defined in the SQL in the db. Clearing Symfony cache is recommended after this step:


symfony cc



Then it's finally time to build Model so that we can use the ORM to access db:


symfony propel:build-model


Note: Actually, you MAY build Model directly from schema.yml, but it'd be pretty pointless without making sure your Model has a working db connection, right?