Symfony查询结果返回空字符串以进行工作查询(查询结果来自DB)

时间:2017-01-30 19:12:09

标签: mysql doctrine-orm doctrine symfony

我正在为我的应用程序构建自定义搜索查询。问题是查询结果是空的,但是当我在phpMyAdmin中运行相同的查询(来自debug的runnable query)时,我得到了预期的结果。

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 debug doctrine log:

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;

修改

这是来自symfony debug的runnable查询,它在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';

更新:

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

1 个答案:

答案 0 :(得分:0)

您在此处提到的调试查询与您的实际教义查询不匹配。 如果你去Symfony profiler&gt; Doctrine,会有一个查询列表。

如果您可以找到与您的学说匹配的一个并在phpmyadmin中运行它或将其粘贴到此处,那么找到问题会很有帮助。