0

symfony 1.4 和教义 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;
    }
}
4

2 回答 2

1

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

当需要连接时,最好使用 queryBuilder 或 DQL。

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

这是 Symfony 教义书中的文档: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();        
于 2013-01-26T13:16:51.397 回答
0

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

如果您不想再从 symfony2(-docs) 和教义 2 中获得愚蠢的解决方案,因为您需要比 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();
于 2013-01-26T18:24:25.443 回答