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?

Wednesday, April 01, 2009

為了某個不足為外人道之原因,在下做了這件感覺起來很蹩腳很挫的事… >_< 真的要拿出去討論的話,可能我還得好一陣子的心理建設才敢拿出去曝光… 但如果有幸有哪位前輩高手不幸在blogger看到這篇的話,還不吝留下您的意見; 像是更好的做法… 等等。 小弟感激不盡。m(_ _)m

以下這一段是要達到效果的描述:
一個母網頁頁面,必須將其cookie丟到其所包含之一個iframe裡所指到的一個不同domain的網頁(姑且稱此網頁為子網頁)。 而子網頁必須要能拿到母網頁的cookie來做一些… 見不得人的事兒。 在開啟母網頁同時,即需可看到母網頁下iframe裡的子網頁根據母網頁的cookie所做出的頁面。


為了做這件事,在擦股溝擦了好一會兒後,所做出的解答如下:

母頁面就只是一個html,除了一些讀寫刪cookie的Javascript functions外,還包括兩個iframe:


<iframe id="test1" src ="http://howard.com/cookiecutter.php?cooke_name=cookie_value" width="300px" height="100px" >


cookiecutter.php:



<?php
header("P3P: CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
$keys = array_keys($_GET);
header("Set-Cookie: ".$keys[0] ."=" . $_GET[$keys[0]]. "; domain=.howard.com; path=/"); ?>

cookiecutter.php較特別的是那一行header P3P的code。 P3P全名為:「The Platform for Privacy Preferences」。 基本上就是跟網頁瀏覽器說,我知道我現在要做的事不合規定或不安全,但你就讓我做就是了。 然後下一行header set-Cookie就在howard.com上設一個名字和值為cooke_name和cookie_value的cookie。


值得注意的是,如果拿掉P3P那一行,剛剛設的這個cookie在母頁上將拿不到。


如此,雖然是達成了目標,但是不管怎麼看,都應該只能算是很粗糙的解決方案,並不是真正的cookie passing。 小弟在此拋磚引玉,還希望各位先進們指教一下。