主要任务是拥有一个实体,可以连接到另一个实体。 我已经有一个名为“档案”的实体,在这一个我喜欢有一个像邮件树形结构。与CRUD的教条/ symfony中的树
- 你会以这种方式存储对话吗?
- 如何使用Doctrine实现树?
- Symfony可以为它生成CRUD吗?
主要任务是拥有一个实体,可以连接到另一个实体。 我已经有一个名为“档案”的实体,在这一个我喜欢有一个像邮件树形结构。与CRUD的教条/ symfony中的树
是的,你可以这样做。
我喜欢生成注释风格,YAML作为第二选择个人。你将如何生成你的实体映射?我相信XML是您的另一种选择。
你的问题似乎是,你可以有一个关系,是的,你可以。你使用FK或Foriegn Keys来做这件事。你有没有看过MySQL,或者你有什么兴趣?
如果你不确定如何设计你的模式,MySQL有一个很好的程序叫做MySQL Workbench。你熟悉吗?您可以使用它来生成一个简单的FK表关系,然后生成这些实体将遵循这个关系。每次重新调整模式时都要重新生成。我认为从模式反向工程实体是最好的方法。
如果你走这条路线,一旦你设计了你的数据库模式,建立的表,如果是空的并不重要,你可以用这种方法对你的实体进行逆向工程。 see this url here
//import orm mapping information from database, and using a filter for a specific table if you wanted
php app/console doctrine:mapping:import --force YourBundle yml --filter Table1
//convert mapping information, only if you didnt use YML in the top command.
//we need to force this, make sure you dont need any changes on the entity page (normally this is left default_, otherwise back it up first.
php app/console doctrine:mapping:convert annotation ./src --force --filter Table1
生成实体现在
$php app/console doctrine:generate:entities YourBundle
// or generate an entity from scratch
$ php app/console doctrine:generate:entity
生成的getter和setter任何新特性,不会碰你的方法,是安全的。
$ php app/console doctrine:generate:entities Acme/YourBundle/Entity/Table1
$ php app/console doctrine:schema:update --force or --dump-sql
这将继续Foriegn关系。当你使用Doctrine来访问你的数据时,你可以使用关系来查询你如何看待适合的问题......我尝试在这个问题上得到一个很好的例子,我确实有一个例子,否则就是一个例子。
/**
* getfruitSubtextures()
* @Route("/fruit/subtextures/color/{color}/size/{size}/texture/{texture}", name="get_fruit_subtextures", options={"expose"=true})
* @param $color
* @param $size
* @param $texture
* @Method("GET")
*/
public function getfruitSubtextures($color, $size, $texture){
$em = $this->getDoctrine()->getManager();
$rsm = new ResultSetfruitpingBuilder($em);
$rsm->addRootEntityFromClassMetadata('WeRuS\fruitBundle\Entity\Opttextures', 'g');
$sql = "SELECT SubtextureLabel, textureSubtexture
FROM opt_textures
WHERE texturelabel = :texturelabel
AND color = :color
AND size = :size
ORDER BY SubtextureLabel ASC";
$query = $em->createNativeQuery($sql,$rsm);
$query->setParameters(array(
'color' => $color,
'size' => $size,
'texturelabel' => $texture,
));
$results = $query->getArrayResult();
if (!$results)
{
throw $this->createNotFoundException('No Matching subtextures found for this fruit size texture!');
}
else
{
$response = new JsonResponse();
$response->setData($results);
return $response;
}
}
就使得它一个CRUD,等到你有你的模式,和实体处于正常工作状态,然后你按照this procedure
$ php app/console generate:doctrine:crud --entity=YourBundle:Table1 --with-write --format=annotation --overwrite
编辑,我吼声用户一直在寻找例如一棵树就像结构树,而不是树,这篇文章主要提供如何在没有树的情况下做到这一点。哎呀。
对于你的第二个问题,看看树的学说扩展:https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md –
也看看这个包:https:// github.com/FriendsOfSymfony/FOSMessageBundle –