小内连接查询

时间:2013-01-26 13:00:38

标签: symfony doctrine-orm

symfony 1.4和doctrine 1.2中的旧查询

$user = Doctrine_Query::create()
                ->from('User.u')
                ->innerJoin('u.State s')
                ->where('u.id = ?', $id)
                ->andWhere('u.state_id = ?', $state_id)
                ->fetchOne();

现在我在symfony2中的查询:

$repository = $this->getDoctrine()
->getRepository('FrontendAccountBundle:User');

$user = $repository->findBy(array(
    'activationId' => $activation_id), 
    array('state' => 3));

我的错误正在出现:

无法识别的字段:状态

有什么问题?

编辑:重新格式化代码

更新

用户实体:

namespace Frontend\AccountBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User implements UserInterface, \Serializable
{

    /**
     * @var string
     *
     * @ORM\Column(name="activation_id", type="string", length=255, nullable=true)
     */
    private $activationId;


    /**
     * @var \State
     *
     * @ORM\ManyToOne(targetEntity="State")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="state_id", referencedColumnName="id")
     * })
     */
    private $state;




    /**
     * Set activationId
     *
     * @param string $activationId
     * @return User
     */
    public function setActivationId($activationId)
    {
        $this->activationId = $activationId;

        return $this;
    }

    /**
     * Get activationId
     *
     * @return string 
     */
    public function getActivationId()
    {
        return $this->activationId;
    }

    /**
     * Set state
     *
     * @param \Frontend\AccountBundle\Entity\State $state
     * @return User
     */
    public function setState(\Frontend\AccountBundle\Entity\State $state = null)
    {
        $this->state = $state;

        return $this;
    }

    /**
     * Get state
     *
     * @return \Frontend\AccountBundle\Entity\State 
     */
    public function getState()
    {
        return $this->state;
    }

    public function __construct()
    {
        $this->isActive = true;
        $this->salt = md5(uniqid(null, true));
    }

    /**
     * @inheritDoc
     */
    public function getUsername()
    {
        return $this->email;
    }
    /**
     * @see \Serializable::serialize()
     */
    public function serialize()
    {
        return serialize(array(
            $this->id,
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id,
        ) = unserialize($serialized);
    }
}

用户实体:

namespace Frontend\AccountBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * State
 *
 * @ORM\Table(name="state")
 * @ORM\Entity
 */
class State
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="state", type="string", length=255, nullable=false)
     */
    private $state;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=255, nullable=false)
     */
    private $description;



    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set state
     *
     * @param string $state
     * @return State
     */
    public function setState($state)
    {
        $this->state = $state;

        return $this;
    }

    /**
     * Get state
     *
     * @return string 
     */
    public function getState()
    {
        return $this->state;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return State
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }
}

2 个答案:

答案 0 :(得分:1)

问题是User实体中的变量是“state”而不是“stateId”。您必须始终使用实体中的名称,而不是数据库。由于stateId位于State实体中,因此还需要完成从User到State的连接。

当需要加入时,最好使用queryBuilder或DQL。

这是一篇关于Doctrine 2 queryBuilder中的连接的帖子:doctrine 2 query builder and join tables

以下是Symfony Book for Doctrine的文档:http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations

以下是我的项目中与您的问题非常相似的示例:

    $uid = 2;
    $rep = $this->getDoctrine()->getRepository('DevondevTrackRTimeBundle:Activity');
    $q = $rep->createQueryBuilder('a')
        ->select ('a.activityId, a.startTime, a.endTime, u.username')
        ->join('a.login','u')
        ->where('u.id = :uid')
        ->setParameter('uid', $uid)
        ->getQuery();

    $acts = $q->getResult();

如果我不需要用户表中的任何内容,则查询可以写为

    $uid = 2;
    $rep = $this->getDoctrine()->getRepository('DevondevTrackRTimeBundle:Activity');
    $q = $rep->createQueryBuilder('a')
        ->where('a.login = :uid')
        ->setParameter('uid', $uid)
        ->getQuery();

    $acts = $q->getResult();

这是你的查询以同样的方式重写:

    $rep = $this->getDoctrine()->getRepository('FrontendAccountBundle:User');
    $q = $rep->createQueryBuilder('u')
        ->join('u.state','s')
        ->where ('u.id = :uid')
        ->andWhere ('s.stateId = :sid')
        ->setParameters(array('uid' => $id, 'sid' => $state_id))             
        ->getQuery(); 

    $user = $q->getSingleResult();        

答案 1 :(得分:0)

感谢彼得点亮了我一点!!!

如果您不想再次使用symfony2(-docs)和doctrine2中的愚蠢解决方案,因为您需要比symfony1.4更多的代码,就像那样http://symfony.com/doc/current/book/doctrine.html#joining-to-related-records。试试我的解决方案。

结果如下。

        $em = $this->getDoctrine()->getEntityManager();
        $user = $em->createQuery('SELECT u FROM FrontendAccountBundle:User u
              INNER JOIN FrontendAccountBundle:State s
                WHERE
                u.activation_id=:activation_id
                and
                u.state=:state_id
                ')
            ->setParameter('activation_id', $activation_id)
            ->setParameter('state_id', 3)
            ->getSingleResult();