Symfony,无法从表中检索记录

时间:2014-12-10 18:49:06

标签: php symfony

在Symfony中,我无法使用 find()从表格中检索记录,但我可以使用 createQuery()?这是我项目中的表格中随机发生的。使用symfony find(),findBy()等似乎无法访问数据,但我可以使用dql ???

为什么会这样?有没有人曾经发生过这种情况?我无法弄清楚这一点。谢谢你的帮助!

测试我已经:我使用完全相同的实体字段创建了一个类似的表,并将数据导入到表中,它工作得很好。为什么这个表只是停止响应Symfony的请求?

此作品

    $dql = "SELECT co FROM WIC\CommonBundle\Entity\CustomOptions co WHERE co.account=:account_id AND co.option_field=:value";
    $query = $em->createQuery($dql);
    $query->setParameters(array(
        'value' => 'reorder_reason',
    ));
    $customOptionValue = $query->getResult();
    echo count($customOptionValue); // equals 3

这不起作用 - 在

中传递完全相同的变量
    $customOptionValue = $em->getRepository('WICCommonBundle:CustomOptions')->findBy(
    array(
           "option_field"=>"reorder_reason",
        )
    );
    echo count($customOptionValue); // equals 0

以下是我的CustomOptions实体:

namespace WIC\CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * CustomOptions
 *
 * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="accountFieldValueOptions", columns={"account_id", "option_value", "option_field"})})
 * @ORM\Entity(repositoryClass="WIC\CommonBundle\Entity\CommonRepository")
 * @Gedmo\Loggable
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 * @ORM\HasLifecycleCallbacks
 */
class CustomOptions
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @var string $option_value
     *
     * @Gedmo\Versioned
     * @ORM\Column(name="option_value", type="string", length=255)
     * @Assert\NotBlank(message="Option Value Should Not Be Blank")
     */
    private $option_value;

    /**
     * @var string $option_field
     *
     * @Gedmo\Versioned
     * @ORM\Column(name="option_field", type="string", length=255, unique=false, nullable=true)
     */
    private $option_field;

    /**
     * @ORM\ManyToOne(targetEntity="WIC\AccountBundle\Entity\Account", fetch="EAGER")
     * @ORM\JoinColumn(name="account_id", referencedColumnName="id", nullable=false)
     * @Gedmo\Versioned
     */
    protected $account;

    /**
     * @var datetime $created
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $created;

    /**
     * @var datetime $updated
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $updated;

    /**
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
     */
    private $deletedAt;


    public function __construct()
    {

    }

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

    /**
     * Set account
     *
     * @param \WIC\AccountBundle\Entity\Account $account
     * @return InventoryLocation
     */
    public function setAccount(\WIC\AccountBundle\Entity\Account $account = null)
    {
        $this->account = $account;

        return $this;
    }

    /**
     * Get account
     *
     * @return \WIC\AccountBundle\Entity\Account
     */
    public function getAccount()
    {
        return $this->account;
    }

    /**
     * Set created
     *
     * @param \DateTime $created
     * @return CustomOptions
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

    /**
     * Get created
     *
     * @return \DateTime
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set updated
     *
     * @param \DateTime $updated
     * @return CustomOptions
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get updated
     *
     * @return \DateTime
     */
    public function getUpdated()
    {
        return $this->updated;
    }

    /**
     * Set deletedAt
     *
     * @param \DateTime $deletedAt
     * @return CustomOptions
     */
    public function setDeletedAt($deletedAt)
    {
        $this->deletedAt = $deletedAt;

        return $this;
    }

    /**
     * Get deletedAt
     *
     * @return \DateTime
     */
    public function getDeletedAt()
    {
        return $this->deletedAt;
    }

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

    /**
     * Set option_value
     *
     * @param string $option_value
     * @return CustomOptions
     */
    public function setOptionValue($option_value)
    {
        $this->option_value = $option_value;
    }

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

    /**
     * Set option_field
     *
     * @param string $option_field
     * @return CustomOptions
     */
    public function setOptionField($option_field)
    {
        $this->option_field = $option_field;
    }

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

    /**
     * Set name
     *
     * @param string $name
     * @return CustomOptions
     */
    public function setName($name)
    {
        $this->name = $name;
    }

1 个答案:

答案 0 :(得分:1)

尝试使用ID而不是整个对象

即。

$customOptionValue = $em->getRepository('WICCommonBundle:CustomOptions')->findBy(array(
    "account"=>$account->getId(), 
    "option_field"=>"reorder_reason",
));

编辑:您未按照symfony预期的编码标准导致此问题:

Use camelCase, not underscores, for variable, function and method names

private $option_field应该变为private $optionField,您应该调整您创建的任何功能以反映这一点。然后你的findBy数组将使用"optionField"=>"reorder_reason"