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);

Monday, April 12, 2010

Font Shadow

Wondering how it's done? Here are the HTML codes:

text shadow


CSS codes:

#text{
font-size: 3em; /* optional. just to increase the font size. */
display: block;
line-height: 1em;
color: #666; /* shadow color */
background-color: transparent;
white-space: nowrap; /* wrapping breaks the effect */
}

#text:before,
#text:after{
content: "text shadow"; /* generated text */
display: block;
}

#text:before{
margin-bottom: -1.05em;
margin-left: 0.1ex;
color: #ccc; /* shadow color */
background-color: transparent;
}

#text:after{
margin-top: -1.05em;
margin-left: -0.1ex;
color: #fff; /* text color */
background-color: transparent;
}

Drop shadow with CSS for all web browsers

One of the most common CSS effects is using shadows in various ways. Before, we needed to resort to images, but now we can offer this to all major web browser with CSS!

Web browser support

Believe me or not, but all of these web browsers we can offer shadows with CSS:

* Firefox 3.5+
* Safari 3+
* Google Chrome
* Opera 10.50
* Internet Explorer 5.5

The standards way

As we all know, a majority of the web browsers implement features in a standardized way, while others don’t (although they are getting better at it). Previously, in the W3C specification CSS Backgrounds and Borders Module Level 3 box-shadow was described, although at the moment, it’s not in there for some things to be discussed further. Anyway, this is how the implementation looks:
view sourceprint?
1..shadow {
2. box-shadow: 3px 3px 4px #000;
3.}

The first value describes the x-offset (could be a negative value as well), the second the y-offset, the third the radius of the shadow and the fourth the color of it. Opera 10.50 (currently only available on Windows) is the first web browser to have an implementation without a vendor-prefix, whereas Firefox, Safari and Google Chrome all need it for now. So, this code makes it work in all those web browsers:
view sourceprint?
1..shadow {
2. -moz-box-shadow: 3px 3px 4px #000;
3. -webkit-box-shadow: 3px 3px 4px #000;
4. box-shadow: 3px 3px 4px #000;
5.}
What about Internet Explorer?

Luckily enough for us, there are a couple of filters we can use to make this work: The DropShadow filter and the Shadow filter. The problem with the DropShadow filter is that the shadow is solid, and not fluffy as desired, although it offers easy values for X and Y. The Shadow filter on the other hand offers a nice shadow, but instead of x and y offset, we need to specify direction and strength the set the length of the shadow.

So, this is how we make it work in Internet Explorer:
view sourceprint?
1..shadow {
2. /* For IE 8 */
3. -ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#000000')";
4. /* For IE 5.5 - 7 */
5. filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#000000');
6.}
The combined styles

This all the CSS for various web browser collected in one rule:
view sourceprint?
01..shadow {
02. -moz-box-shadow: 3px 3px 4px #000;
03. -webkit-box-shadow: 3px 3px 4px #000;
04. box-shadow: 3px 3px 4px #000;
05. /* For IE 8 */
06. -ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#000000')";
07. /* For IE 5.5 - 7 */
08. filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#000000');
09.}