在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
这是我的自定义选项实体:
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;
}
尝试使用 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"
我看到你使用的是软@Gedmo'SoftDeleteable
.您能否检查是否存在且无法检索的记录没有设置deletedAt
?
原则查询忽略了设置删除的记录。但是,假设"不是教义查询"仍然能够找到这些记录。