【问题标题】:Symfony, Cannot retrieve record from tableSymfony,无法从表中检索记录
【发布时间】:2015-02-09 02:17:17
【问题描述】:

在 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;
    }

【问题讨论】:

  • 我正在跑去开会,看了不到 20 秒,但可能是 "account"=>$account->getId()?
  • 我删除了账号id,对问题没有影响。
  • 没有人要求您删除任何内容。他指出您正在通过对象而不是 ID 查找它。在 findBy 方法中,数组中的键仅用于学说中的映射,即帐户映射到 account_id,因此它在做一个没有意义的 where account_id=YOUR_PHP_OBJECT。
  • 我不在乎是否没有人要求我不要删除任何东西,变量与原因或解决方案无关,显然已经对问题产生了两个不准确的回答。干杯。
  • 好吧,这件事的美妙之处在于,您现在已经学会了两件完全错误的事情,而不仅仅是一件:p 干杯,先生。

标签: php symfony


【解决方案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"

【讨论】:

  • 我删除了账号id,对问题没有影响。
  • 我不在乎是否没有人要求我不要删除任何东西,变量与原因或解决方案无关,显然已经对问题产生了两个不准确的回答。干杯。我“从字面上”复制并粘贴了您的代码,但根本没有解决问题。
  • 已编辑答案,因为错误是由于您没有按照 symfony2 预期的命名约定映射变量。
  • 它不起作用。这是一个比你提出的更深层次的问题。我已经尝试了您的建议,但没有解决方案。
  • 我使用完全相同的实体字段创建了一个类似的表,并将数据导入到表中,它工作得非常好。为什么这个表刚刚停止响应 Symfony 的请求???让我们走的东西嗯???
【解决方案2】:

我看到您使用的是软@Gedmo\SoftDeleteable。请检查存在且无法检索的记录是否没有设置deletedAt

Doctrine 查询忽略设置了 deletedAt 的记录。但是假设“非教条查询”仍然能够找到这些记录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多