私のアプリケーションのカスタム検索クエリを構築しています。問題はクエリの結果が空であることですが、私はphpMyAdminで同じクエリ(デバッグから実行可能なクエリ)を実行すると、私は期待される結果を得ました。symfonyのクエリ結果は、動作中のクエリ(DBからの結果を含むクエリ)の空の文字列を返します。
は、問題がどこにあるsomeoenが知ってい:
class ProductRepository extends EntityRepository
{
public function findByParameters($searchParameters)
{
$condition = $this->createQueryBuilder('p')
->where('p.state LIKE :state')
->setParameter('state', 0);
if(isset ($searchParameters["title"])){
$condition
->andWhere('p.title LIKE :title')
->setParameter('title', $searchParameters["title"]);
}
if(isset ($searchParameters["categories"])){
$condition
->andWhere('p.categories IN (:categories)')
->setParameter('categories', $searchParameters["categories"]);
}
if(isset ($searchParameters["productCondition"])){
$condition
->andWhere('p.productCondition = :productCondition')
->setParameter('productCondition', $searchParameters["productCondition"]);
}
$condition
->getQuery()
->getResult();
return $condition;
}
}
symfonyのデバッグ教義ログ:
SELECT t0.id AS id_1, t0.name AS name_2, t0.surname AS surname_3, t0.username AS username_4, t0.password AS password_5, t0.email AS email_6, t0.created AS created_7, t0.roles AS roles_8 FROM users t0 WHERE t0.id = ?
Parameters: [0 => 5]
Hide formatted query Hide runnable query Explain query
SELECT
t0.id AS id_1,
t0.name AS name_2,
t0.surname AS surname_3,
t0.username AS username_4,
t0.password AS password_5,
t0.email AS email_6,
t0.created AS created_7,
t0.roles AS roles_8
FROM
users t0
WHERE
t0.id = ?
SELECT t0.id AS id_1, t0.name AS name_2, t0.surname AS surname_3, t0.username AS username_4, t0.password AS password_5, t0.email AS email_6, t0.created AS created_7, t0.roles AS roles_8 FROM users t0 WHERE t0.id = 5;
EDIT:
これが実行可能である。ここ
コードですsymfonyデバッグからのクエリですphpMyAdminの中で働い:
SELECT p0_.id AS id_0, p0_.state AS state_1, p0_.title AS title_2, p0_.content AS content_3, p0_.price AS price_4, p0_.images AS images_5, p0_.created_at AS created_at_6, p0_.updated_at AS updated_at_7, p0_.expires_at AS expires_at_8, p0_.condition_id AS condition_id_9, p0_.user_id AS user_id_10 FROM product p0_ WHERE p0_.state LIKE 0 AND p0_.title LIKE 'Pc';
UPDATE:
I checked Doctrine logs and I am getting this error too:
AppBundle\Entity\Product
The association AppBundle\Entity\Product#productCondition refers to the inverse side field AppBundle\Entity\ProductCondition#products which does not exist.
The association AppBundle\Entity\Product#user refers to the inverse side field AppBundle\Entity\User#products which does not exist.
製品エンティティ:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
* @ORM\HasLifecycleCallbacks koristi se za pokretanje određenih događaja svaki put kada entitet doživi neki lifecycle
*/
class Product implements ProductInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="state", type="integer")
*/
protected $state;
/**
* @ORM\ManyToMany(targetEntity="Category")
* @ORM\JoinTable(name="product_category",
* joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=true)},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")},
*)
*
*
* @var ArrayCollection
*/
protected $categories;
/**
* @ORM\ManyToOne(targetEntity="ProductCondition", inversedBy="products", cascade={"persist"})
* @ORM\JoinColumn(name="condition_id", referencedColumnName="id", nullable=true)
*
* @var AppBundle\Entity\ProductConditionInterface
*/
protected $productCondition;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="products", cascade={"persist"})
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
*
* @var AppBundle\Entity\User
*/
protected $user;
/**
* @var string $title
*
* @ORM\Column(name="title", type="string", length=50)
* @Assert\NotBlank()
* @Assert\Length(
* min = 2,
* max = 50,
* minMessage = "Title must be at least {{ limit }} characters long",
* maxMessage = "Title cannot be longer than {{ limit }} characters"
*)
*/
private $title;
/**
* @var text $content
*
* @ORM\Column(name="content", type="text", nullable=true)
*/
private $content;
/**
* @var float price
*
* @ORM\Column(name="price", type="decimal", precision=12, scale=2)
*/
private $price;
/**
* @ORM\Column(type="array", nullable=true)
*/
protected $images;
/**
* @ORM\Column(type="datetime", nullable=false)
*
* @var \DateTime
*/
protected $createdAt;
/**
* @ORM\Column(type="datetime", nullable=false)
*
* @var \DateTime
*/
protected $updatedAt;
/**
* @ORM\Column(type="datetime", nullable=false)
*
* @var \DateTime
*/
protected $expiresAt;
public function __construct()
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
$this->expiresAt = new \DateTime();
$this->expiresAt->modify('+1 week');
$this->state = 0;
$this->categories = new ArrayCollection();
$this->images = array();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set state
*
* @param int $state
*/
public function setState($state)
{
$this->state = $state;
}
/**
* Get state
*
* @return int
*/
public function getState()
{
return $this->state;
}
/**
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function getCategories()
{
return $this->categories;
}
/**
* @param AppBundle\Entity\CategoryInterface $category
*
* @return AppBundle\Entity\ProductInterface
*/
public function addCategory(CategoryInterface $category)
{
$this->categories->add($category);
}
/**
* @param AppBundle\Entity\CategoryInterface $category
*
* @return AppBundle\Entity\ProductInterface
*/
public function removeCategory(CategoryInterface $category)
{
$this->categories->removeElement($category);
}
/**
* @return AppBundle\Entity\ProductConditionInterface
*/
public function getProductCondition()
{
return $this->productCondition;
}
/**
* @param AppBundle\Entity\ProductConditionInterface $productCondition
*
* @return \AppBundle\Entity\ProductConditionInterface
*/
public function setProductCondition(ProductConditionInterface $productCondition)
{
$this->productCondition = $productCondition;
}
/**
* @param AppBundle\Entity\User
*
*/
public function setUser(User $user = null)
{
$this->user = $user;
}
/**
* @return AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* @param AppBundle\Entity\ProductConditionInterface $productCondition
*
* @return \AppBundle\Entity\ProductConditionInterface
*/
/**
* Set content
*
* @param string $content
*/
public function setContent($content)
{
$this->content = $content;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set content
*
* @param float $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* Get content
*
* @return float $price
*/
public function getPrice()
{
return $this->price;
}
/**
* set images
*
*/
public function setImages($images)
{
$this->images = $images;
}
/**
* Get images
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
/**
* Set createdAt
*
* @param DateTime $created_at
*/
public function setCreatedAt(\DateTime $created_at)
{
$this->createdAt = $created_at;
}
/**
* Get createdAt
*
* @return DateTime
*/
public function getCreatedAt() : \DateTime
{
return $this->createdAt;
}
/**
* Get getUpdatedAt
*
* @return DateTime
*/
public function getUpdatedAt() : \DateTime
{
return $this->updatedAt;
}
/**
* Set $updatedAt
*
* @param DateTime $updated_at
*/
public function setUpdatedAt(\DateTime $updated_at)
{
$this->updatedAt = $updated_at;
}
/**
* Get ExpiresAt
*
* @return DateTime
*/
public function getExpiresAt() : \DateTime
{
return $this->expiresAt;
}
/**
* Set $expiredAt
*
* @param DateTime $expired_at
*/
public function setExpiresAt(\DateTime $expired_at)
{
$this->expiredAt = $expired_at;
}
}
なぜクエリの結果の代わりにクエリビルダーを返すのですか? – Cerad
@Cerad私はクエリの結果を返しています($条件 - > getQuery() - > getResult();) – user2496520
return $ condition;そうでなければ示唆する。 – Cerad