In the 学说文档 http://doctrine-orm.readthedocs.org/en/latest/tutorials/getting-started.html#starting-with-the-product有这样的代码:
<?php
// update_product.php <id> <new-name>
require_once "bootstrap.php";
$id = $argv[1];
$newName = $argv[2];
$product = $entityManager->find('Product', $id);
if ($product === null) {
echo "Product $id does not exist.\n";
exit(1);
}
$product->setName($newName);
$entityManager->flush();
我不明白的是最后一部分,在设置产品名称后$product->setName()
, the $entityManager->flush()
方法称为:
$product->setName($newName);
$entityManager->flush();
在我看来,两者之间没有任何联系$product
变量和$entityManager
变量,除了以下事实:$product
应包含来自$entityManager->find()
method.
怎么可能$entityManager->flush()
可以读取设置的值$product->setName()
?
It's a ORM
魔法 :)
但如果认真的话,当你使用获取数据时Doctrine
,它向您的对象添加了大量元数据。这些字段你可以自己看一下var_dump()
一个东西。
当你做flush()
, Doctrine
检查所有获取数据的所有字段并向数据库进行事务处理。
当你初始化一个新对象时,它没有任何Doctrine
元数据,因此您必须再调用一种方法persist()
添加它。
您也可以只看源代码 http://www.doctrine-project.org/api/orm/2.0/source-class-Doctrine.ORM.EntityManager.html#32-751 of EntityManager
为了更好地理解它是如何工作的——Doctrine
是一个开源项目。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)