我很难理解Doctrine手册对级联操作的解释,需要有人帮助我理解简单的ManyToOne关系中的选项.
在我的应用程序中,我有一个名为Article的表/实体,它有一个外键字段,引用名为Topic的表/实体中的'id'字段.
当我创建新文章时,我从下拉菜单中选择主题.这会在Article表的'topic_id'外键字段中插入一个整数.
我在Article实体中设置了$ topic关联,如下所示:
/**
* @ManyToOne(targetEntity="Topic")
* @JoinColumn(name="topic_id", referencedColumnName="id", nullable=false)
*/
private $topic;
Run Code Online (Sandbox Code Playgroud)
主题实体没有关于Article实体的任何往复注释.当删除引用主题的文章时,主题不关心什么文章引用它们,并且不需要在主题中发生任何事情.
因为我没有在Article实体中指定级联操作,所以当我尝试创建新文章时,Doctrine会抛出错误:"通过未配置为级联持久操作的关系找到新实体.明确地保留新实体或者在关系上配置级联持久化操作."
所以我知道我需要选择一个级联操作来包含在Article实体中,但是我怎么知道在这种情况下选择哪个操作呢?
从阅读Doctrine手册开始,"分离"听起来就像是正确的选择.但是在这里和这里研究其他人的类似问题让我觉得我想用"坚持"代替.
任何人都可以帮助我理解"持久","删除","合并"和"分离"是否意味着简单的ManyToOne关系,就像我所描述的那样?
我需要插入一个具有关联的实体.
如果我已经拥有关联实体的FK,是否有办法将主要实体插入数据库中,只填充FK?
或者我总是必须这样做
有人能告诉我如何在datetime列上添加默认值吗?我不能这样做:
protected $registration_date = date("Y-m-d H:i:s", time());
Run Code Online (Sandbox Code Playgroud)
又怎样?
我想让字段updated_at和created_at我的Doctrine实体自动更新.
在Ruby on Rails模型中有2个字段:updated_at和created_at.
可在此处找到说明:http://guides.rubyonrails.org/migrations.html#migration-overview:
时间戳宏添加了两列,created_at和updated_at.这些特殊列由Active Record自动管理(如果存在).
我可以在Doctrine 2中启用类似的功能吗?
文档或我有问题.我做了文档所说的所有内容.
当我输入终端时:
$ php vendor/bin/doctrine orm:schema-tool:create
Run Code Online (Sandbox Code Playgroud)
输出是:
No Metadata Classes to process
Run Code Online (Sandbox Code Playgroud)
我读了许多帖子,并谷歌尝试了许多例子,但没有.
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/getting-started.html
我有一个ID数组,我想从我的ID数组中获取一个实体数组.
我不能用find.
sql请求如下所示:
SELECT * FROM mytable WHERE id = 12 OR id = 10 ...
Run Code Online (Sandbox Code Playgroud)
我的id数组上有一个循环.
我有一个父结构的树结构.目前我正在尝试让所有父节点显示当前节点的路径.
基本上我正在做一个while循环来处理所有节点.
$current = $node->getParent();
while($current) {
// do something
$current = $current->getParent();
}
Run Code Online (Sandbox Code Playgroud)
使用默认findById方法有效.因为实体有一些聚合字段,我使用自定义存储库方法,用一个查询加载所有基本字段.
public function findNodeByIdWithMeta($id) {
return $this->getEntityManager()
->createQuery('
SELECT p, a, c, cc, ca, pp FROM
TestingNestedObjectBundle:NestedObject p
JOIN p.actions a
LEFT JOIN p.children c
LEFT JOIN c.children cc
LEFT JOIN c.actions ca
LEFT JOIN p.parent pp
WHERE p.id = :id
')
->setParameter('id', $id)
->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
)
->getOneOrNullResult();
}
Run Code Online (Sandbox Code Playgroud)
使用该代码,加载父项失败.我只得到直接的父母(由LEFT JOIN p.parent pp他们解决)而不是上面的父母.例如$node->getParent()->getParent() …
在Doctrine2中你用什么而不是ENUM?SMALLINT?我想过使用varchar,或者明确定义char,但是当涉及到索引时,这可能不是很有效,或者我错了?
我正在搜索,无法找到答案.我的应用程序中有数据库角色模型.用户可以拥有角色,但必须将此角色存储到数据库中.
但是,用户需要从数据库中添加默认角色.所以我创建了一个服务:
<?php
namespace Alef\UserBundle\Service;
use Alef\UserBundle\Entity\Role;
/**
* Description of RoleService
*
* @author oracle
*/
class RoleService {
const ENTITY_NAME = 'AlefUserBundle:Role';
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function findAll()
{
return $this->em->getRepository(self::ENTITY_NAME)->findAll();
}
public function create(User $user)
{
// possibly validation here
$this->em->persist($user);
$this->em->flush($user);
}
public function addRole($name, $role) {
if (($newrole = findRoleByRole($role)) != null)
return $newrole;
if (($newrole = findRoleByName($name)) != null)
return $newrole;
//there is no existing role …Run Code Online (Sandbox Code Playgroud) 我想使用Doctrine的查询构建器构造以下SQL:
select c.*
from customer c
join phone p
on p.customer_id = c.id
and p.phone = :phone
where c.username = :username
Run Code Online (Sandbox Code Playgroud)
首先我试过了
$qb->select('c')
->innerJoin('c.phones', 'p', Join::ON, $qb->expr()->andx(
$qb->expr()->eq('p.customerId', 'c.id'),
$qb->expr()->eq('p.phone', ':phone')
))
->where('c.username = :username');
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误
Error: expected end of string, got 'ON'
Run Code Online (Sandbox Code Playgroud)
然后我试了一下
$qb->select('c')
->innerJoin('c.phones', 'p')
->where('c.username = :username')
->andWhere('p.phone = :phone');
Run Code Online (Sandbox Code Playgroud)
这似乎有效.但是,有人知道第一次尝试有什么问题吗?我想让第一个工作,因为它更接近于SQL的结构.提前致谢!
注意:我知道我们也可以使用Doctrine编写本机mysql或dql,但我更喜欢查询构建器.
编辑:下面是整个代码
namespace Cyan\CustomerBundle\Repository;
use Cyan\CustomerBundle\Entity\Customer;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join;
class CustomerRepository extends EntityRepository
{
public function findCustomerByPhone($username, $phone)
{
$qb = $this->createQueryBuilder('c');
$qb->select('c')
->innerJoin('c.phones', …Run Code Online (Sandbox Code Playgroud)