Wednesday, April 14, 2010

Symfony中CREATE、RETRIEVE、UPDATE、DELETE的使用

Symfony中CREATE、RETRIEVE、UPDATE、DELETE的使用

关键词: CREATE RETRIEVE UPDATE DELETE


C.R.U.D. 使用

CREATE

Simple INSERT


/* initialize Propel, etc. */



$author = new Author();

$author->setFirstName("Jack");

$author->setLastName("London");

$author->save();


Related Row Insert


/* initialize Propel, etc. */



// 1) Create an Author (row of 'author' table)



include_once 'bookstore/Author.php';



$author = new Author();

$author->setFirstName("Leo");

$author->setLastName("Tolstoy");

// note: we don't save this yet



// 2) Create a Publisher (row of 'publisher' table)



include_once 'bookstore/Publisher.php';



$pub = new Publisher();

$pub->setName("Viking Press");

// note: we don't save this yet



// 3) Create a Book (row of 'book' table)



include_once 'bookstore/Book.php';



$book = new Book();

$book->setTitle("War & Peace");

$book->setIsbn("0140444173");

$book->setPublisher($pub);

$book->setAuthor($author);

$book->save(); // saves all 3 objects!


RETRIEVE

Retrieving by Primary Key

Single-Col PK


$firstBook = BookPeer::retrieveByPK(1);

// now $firstBook is a Book object, or NULL if no match was found.


Getting Multiple Objects By PK


$selectedBooks = BookPeer::retrieveByPKs(array(1,2,3,4,5,6,7));

// $selectedBooks is an array of Book objects


Multi-Col PK


$myObject = MultiColPKExamplePeer::retrieveByPK(1,2);


Querying the Database

Simple Criteria


$c = new Criteria();

$c->add(AuthorPeer::FIRST_NAME, "Karl");

$c->add(AuthorPeer::LAST_NAME, "Marx", Criteria::NOT_EQUAL);



$authors = AuthorPeer::doSelect($c);


相当于


SELECT ... FROM author WHERE author.FIRST_NAME = 'Karl' AND author.LAST_NAME <> 'Marx';


$c = new Criteria();

$c->add(AuthorPeer::LAST_NAME, array("Tolstoy", "Dostoevsky", "Bakhtin"), Criteria::IN);



$authors = AuthorPeer::doSelect($c);

// $authors contains array of Author objects


相当于


SELECT ... FROM author WHERE author.LAST_NAME IN ('Tolstoy', 'Dostoevsky', 'Bakhtin');


Logically Complex Criteria (AND / OR)


$c = new Criteria();

$cton1 = $c->getNewCriterion(AuthorPeer::FIRST_NAME, "Leo");

$cton2 = $c->getNewCriterion(AuthorPeer::LAST_NAME, array("Tolstoy", "Dostoevsky", "Bakhtin"), Criteria::IN);



// combine them

$cton1->addOr($cton2);



// add to Criteria

$c->add($cton1);


相当于


SELECT ... FROM author WHERE (author.FIRST_NAME = 'Leo' OR author.LAST_NAME IN ('Tolstoy', 'Dostoevsky', 'Bakhtin'));


$c = new Criteria();

$cton1 = $c->getNewCriterion(AuthorPeer::FIRST_NAME, "Leo");

$cton2 = $c->getNewCriterion(AuthorPeer::FIRST_NAME, "Karl");



// combine them

$cton1->addOr($cton2);



// add to Criteria

$c->add($cton1);


或者


$c = new Criteria();

$c->add(AuthorPeer::FIRST_NAME, "Leo");

$c->addOr(AuthorPeer::FIRST_NAME, "Karl");


直接使用 SQL语句


$con = Propel::getConnection(DATABASE_NAME);



$sql = "SELECT books.* FROM books WHERE NOT EXISTS (SELECT id FROM review WHERE book_id = book.id)";

$stmt = $con->createStatement();

$rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM);



$books = BookPeer::populateObjects($rs);


UPDATE

Single Table


// 1) Fetch an object by primary key



$myBook = BookPeer::retrieveByPK(1);



// 2) update the values & save() it.



$myBook ->setTitle("War & Peace");

$myBook->save();


Relation Tables


/* initialize Propel, etc. */



// 1) retrieve an Author

$author = AuthorPeer::retrieveByPK(1);



// 2) retrieve a Book

$book = BookPeer::retrieveByPK(1);



// 3) now blindly set $author as the author for $book!



$book->setAuthor($author);

$book->save();


DELETE

Level 3 Headline

Using Peer

以键删除


BookPeer::doDelete(1);


以类对象删除


$book = BookPeer::retrieveByPK(1);

BookPeer::doDelete($book);


Using Object


$book = BookPeer::retrieveByPK(1);

$book->delete();

// (and now you must remember that you can no longer use the $book object)




$c = new Criteria();

$sc1 = $c->getNewCriterion(BlogPeer::CREATED_AT,'2009-2-10' ,Criteria::LESS_THAN);
$sc2 = $c->getNewCriterion(BlogPeer::CREATED_AT,'2009-3-10' ,Criteria::GREATER_THAN);
$sc3 = $c->getNewCriterion(BlogPeer::ACTIVE,1);
$sc1->\ addOr($sc2);
$sc3-> addAnd($sc1);
$c-> add($sc3);
return BlogPeer::doSelect($c);

No comments: