【发布时间】: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