【问题标题】:In Symfony-Sonata all fields are sortable except for one在 Symfony-Sonata 中,所有字段都是可排序的,除了一个
【发布时间】:2016-06-23 11:21:35
【问题描述】:

Sonata 中,我创建了几个列表并且一切正常。 (请不要说那是不久前的事,所以我可能在那里做了一些事情来解决我将在这里描述的问题......)。

现在我创建了一个新的播放列表实体列表:

如图所示,“Id”列和“Aangemaakt op”列都是可排序的,但“Playlist”列不是。

“Aangemaakt op”和“Playlist”字段都是日期字段,但由于“Aangemaakt op”字段是可排序的,我想说这与它无关。 p>

我一直在搜索 Sonata 文档、Google 和 StackOverflow,但没有找到有关此问题的任何线索。我确实找到了关于基于实体字段对列表进行排序的线程,但我的字段不是实体。

相关代码:

/**
 * @param ListMapper $listMapper
 */
protected function configureListFields(ListMapper $listMapper) {
    $listMapper
        ->add('id')
        ->add('playlist_date', 'date', array('label' => 'Playlist'))
        ->add('created', 'datetime', array('label' => 'Aangemaakt op'))
        ->add(
            '_action', 'actions', array(
                'actions' => array(
                    'delete' => array(),
                )
            )
        );
}

一些 StackOverflow 线程和下面的答案提到将 'sortable' => true 添加到必须可排序的字段中。

这样做确实使该列可单击以进行排序,但单击它会导致以下异常:

Catchable Fatal Error: Argument 1 passed to
Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery::entityJoin() 
must be of the type array, null given, called in
/path/of/my/project/sonata-project/doctrine-orm-admin-bundle/Datagrid/ProxyQuery.php
on line 142 and defined.

根据其他 StackOverflow 线程,这是因为必须创建连接。但是,该字段只是与其他字段相同的Mysql记录的字段。我确实找到了一个 StackOverflow 线程也提到了这一点,并且他们在其中加入了相同的记录以完成这项工作,但我没有让它发挥作用。此外,我感谢这不应该是对列内容进行排序的方式。

有人知道吗?

针对 Hibatallah Aouadni 的回答进行更新

按照 Hibatallah 的建议,我在 PlaylistsAdmin 中添加了以下内容:

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('id')
        ->add('playlist_date')
        ->add('created');
}

这导致以下错误消息:

Notice: Undefined index: playlist_date

所以我检查了我的实体,发现它有一个 UniqueConstraint:

uniqueConstraints={@ORM\UniqueConstraint(name="playlist_date", columns={"playlist_date"})}

它没有定义实际的“索引”,但它当然是。但是作为测试,我添加了以下内容:

, indexes={@ORM\Index(name="playlist_date", columns={"playlist_date"})}

这并没有给出任何不同的结果。 所以仍然没有运气:(

** 实体和实体管理员 **

实体管理员:

<?php

namespace Company\AdminBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Route\RouteCollection;

class PlaylistsAdmin extends Admin
{
    protected $baseRoutePattern = 'playlists';
    protected $baseRouteName = 'playlists';

    protected function configureRoutes(RouteCollection $collection) {
        $collection->clearExcept(array('list', 'delete', 'show'));
    }

    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper) {
        $listMapper
            ->add('id')
            ->add('playlist_date', 'date', array('label' => 'Playlist'))
            ->add('created', 'datetime', array('label' => 'Aangemaakt op'))
            ->add(
                '_action', 'actions', array(
                'actions' => array(
                    'show' => array(),
                    /*'edit' => array(),*/
                    'delete' => array(),
                )
            )
            );
    }

    public function getBatchActions() {
        return array();
    }
}

实体:

<?php

namespace Company\AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Playlist
 *
 * @ORM\Table(name="playlist", uniqueConstraints={@ORM\UniqueConstraint(name="playlist_date", columns={"playlist_date"})})
 * @ORM\Entity()
 */
class Playlist
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="playlist_date", type="date", nullable=true)
     */
    protected $playlistDate;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=true)
     */
    protected $created;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    protected $video;

    /**
     * Constructor
     */
    public function __construct() {
        $this->video = new \Doctrine\Common\Collections\ArrayCollection();
        $this->setCreated(new \DateTime());
    }

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

    /**
     * Set playlistDate
     *
     * @param \DateTime $playlistDate
     * @return Playlist
     */
    public function setPlaylistDate($playlistDate) {
        $this->playlistDate = $playlistDate;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Add video
     *
     * @param \Company\AppBundle\Entity\Video $video
     * @return Playlist
     */
    public function addVideo(\Company\AppBundle\Entity\Video $video) {
        $this->video[] = $video;

        return $this;
    }

    /**
     * Remove video
     *
     * @param \Company\AppBundle\Entity\Video $video
     */
    public function removeVideo(\Company\AppBundle\Entity\Video $video) {
        $this->video->removeElement($video);
    }

    /**
     * Get video
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getVideo() {
        return $this->video;
    }
}

【问题讨论】:

  • 你试过了吗 ->add('playlist_date', 'date', array('sortable' => true)) ?
  • 是的,我做到了。我更新了我的问题,提到了这个解决方案和将抛出的异常。

标签: php sonata-admin symfony-sonata


【解决方案1】:

终于找到了,太荒谬了,在configureListFields方法中,你必须用属性名而不是DataBase名来调用属性,所以:

改变

->add('playlist_date', 'date', array('label' => 'Playlist'))

->add('playlistDate', 'date', array('label' => 'Playlist'))

:D 我不敢相信我们把所有时间都花在了一些荒谬的错误上;)

【讨论】:

  • LOL 感谢您深入研究它。就是这样!!确实很荒谬,我们没有看到这一点。再次感谢。
【解决方案2】:

您需要做的就是将可排序的参数添加到数组中,并将 true 作为值:

->add('playlist_date', 'date', array(
   'label' => 'Playlist',
   'sortable' => true
))

【讨论】:

  • 感谢您的帮助,但这不起作用(还)。我更新了我的问题,提到应用您的解决方案时会引发异常。
  • @vrijdenker 你能不能试着把“日期”改成空并让我更新
  • 导致“可捕获的致命错误:DateTime 类的对象无法转换为字符串”
  • @vrijdenker 你能把它改成“日期时间”吗?什么给了你?
  • 该字段的输出添加了“00:00:00”,但该字段仍然不可排序。
【解决方案3】:

尝试在您的实体管理员的 configureDatagridFilters 中添加播放列表字段:

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('id')
        ->add('playlist_date')
        ->add('created');
}

它会起作用的;)

【讨论】:

  • 感谢您的见解。但是,我没有运气。我更新了我的帖子,解释了我做了什么。
  • @vrijdenker 你能告诉我你的实体(类)吗?如果你没问题,entityAdmin,我们可能会找到一些东西
  • 当然!我刚刚将它们添加到帖子中。
猜你喜欢
  • 2017-08-02
  • 2015-05-04
  • 2016-05-21
  • 1970-01-01
  • 2019-04-12
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多