向symfony 3添加数据表-数据未显示

向symfony 3添加数据表-数据未显示,symfony,datatable,Symfony,Datatable,我尝试将datatables安装到我的Symfony 3项目中。我是新手,不知道哪里出了问题,但我的MySQL数据库中的数据没有显示出来 在我的索引页上有{{datatable\u render(datatable)}},但它显示的所有内容都显示在一个按钮中,用于添加另一个条目 这是我的控制器 namespace AppBundle\Controller; use AppBundle\Entity\Shrubs; use Symfony\Bundle\FrameworkBundle\Contr

我尝试将datatables安装到我的Symfony 3项目中。我是新手,不知道哪里出了问题,但我的MySQL数据库中的数据没有显示出来

在我的索引页上有
{{datatable\u render(datatable)}}
,但它显示的所有内容都显示在一个按钮中,用于添加另一个条目

这是我的控制器

namespace AppBundle\Controller;

use AppBundle\Entity\Shrubs;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Symfony\Component\HttpFoundation\Request;

/**
 * Shrub controller.
 *
 * @Route("shrubs")
 */
class ShrubsController extends Controller
{
    /**
     * Lists all shrub entities.
     *
     * @Route("/", name="shrubs_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $datatable = $this->get('app.datatable.shrubs');
        $datatable->buildDatatable();
       // $em = $this->getDoctrine()->getManager();

   // $datatable = $em->getRepository('AppBundle:Shrubs')->findAll();

    return $this->render('shrubs/index.html.twig', array(
        'datatable' => $datatable,


       // 'shrubs' => $shrubs,

    ));
}
/**
 * @Route("/results", name="shrubs_results")
 */
public function indexResultsAction()
{
    $datatable = $this->get('app.datatable.shrubs');
    $datatable->buildDatatable();

    $query = $this->get('shrubs_datatable')->getQueryFrom($datatable);

    return $query->getResponse();
}
灌木丛数据库

 <?php

namespace AppBundle\Datatables;

use Sg\DatatablesBundle\Datatable\View\AbstractDatatableView;
use Sg\DatatablesBundle\Datatable\View\Style;

/**
 * Class ShrubsDatatable
 *
 * @package AppBundle\Datatables
 */
class ShrubsDatatable extends AbstractDatatableView
{
    /**
     * {@inheritdoc}
     */
    public function buildDatatable(array $options = array())
    {
        $this->topActions->set(array(
            'start_html' => '<div class="row"><div class="col-sm-3">',
            'end_html' => '<hr></div></div>',
            'actions' => array(
                array(
                    'route' => $this->router->generate('shrubs_new'),
                    'label' => $this->translator->trans('datatables.actions.new'),
                    'icon' => 'glyphicon glyphicon-plus',
                    'attributes' => array(
                        'rel' => 'tooltip',
                        'title' => $this->translator->trans('datatables.actions.new'),
                        'class' => 'btn btn-primary',
                        'role' => 'button'
                    ),
                )
            )
        ));

        $this->features->set(array(
            'auto_width' => true,
            'defer_render' => false,
            'info' => true,
            'jquery_ui' => false,
            'length_change' => true,
            'ordering' => true,
            'paging' => true,
            'processing' => true,
            'scroll_x' => false,
            'scroll_y' => '',
            'searching' => true,
            'state_save' => false,
            'delay' => 0,
            'extensions' => array(),
            'highlight' => false,
            'highlight_color' => 'red'
        ));

        $this->ajax->set(array(
            'url' => $this->router->generate('shrubs_index'),
            'type' => 'GET',
            'pipeline' => 0
        ));

        $this->options->set(array(
            'display_start' => 0,
            'defer_loading' => -1,
            'dom' => 'lfrtip',
            'length_menu' => array(10, 25, 50, 100),
            'order_classes' => true,
            'order' => array(array(0, 'asc')),
            'order_multi' => true,
            'page_length' => 10,
            'paging_type' => Style::FULL_NUMBERS_PAGINATION,
            'renderer' => '',
            'scroll_collapse' => false,
            'search_delay' => 0,
            'state_duration' => 7200,
            'stripe_classes' => array(),
            'class' => Style::BOOTSTRAP_3_STYLE,
            'individual_filtering' => false,
            'individual_filtering_position' => 'head',
            'use_integration_options' => true,
            'force_dom' => false,
            'row_id' => 'id'
        ));

        $this->columnBuilder
            ->add('botanicalname', 'column', array(
                'title' => 'Botanicalname',
            ))
            ->add('commonname', 'column', array(
                'title' => 'Commonname',
            ))
            ->add('borderlinehardy', 'column', array(
                'title' => 'Borderlinehardy',
            ))
            ->add('phpreference', 'column', array(
                'title' => 'Phpreference',
            ))
            ->add('wetsoil', 'column', array(
                'title' => 'Wetsoil',
            ))
            ->add('moistsoil', 'column', array(
                'title' => 'Moistsoil',
            ))
            ->add('peatysoil', 'column', array(
                'title' => 'Peatysoil',
            ))
            ->add('welldrainedsoil', 'column', array(
                'title' => 'Welldrainedsoil',
            ))
            ->add('drought', 'column', array(
                'title' => 'Drought',
            ))
            ->add('claysoil', 'column', array(
                'title' => 'Claysoil',
            ))
            ->add('sandysoil', 'column', array(
                'title' => 'Sandysoil',
            ))
            ->add('loamsoil', 'column', array(
                'title' => 'Loamsoil',
            ))
            ->add('infertilesoil', 'column', array(
                'title' => 'Infertilesoil',
            ))
            ->add('richsoil', 'column', array(
                'title' => 'Richsoil',
            ))
            ->add('compactedsoil', 'column', array(
                'title' => 'Compactedsoil',
            ))
            ->add('cityconditions', 'column', array(
                'title' => 'Cityconditions',
            ))
            ->add('pollution', 'column', array(
                'title' => 'Pollution',
            ))
            ->add('salt', 'column', array(
                'title' => 'Salt',
            ))
            ->add('windy', 'column', array(
                'title' => 'Windy',
            ))
            ->add('shade', 'column', array(
                'title' => 'Shade',
            ))
            ->add('partshade', 'column', array(
                'title' => 'Partshade',
            ))
            ->add('fullsun', 'column', array(
                'title' => 'Fullsun',
            ))
            ->add('pestproblem', 'column', array(
                'title' => 'Pestproblem',
            ))
            ->add('id', 'column', array(
                'title' => 'Id',
            ))
            ->add(null, 'action', array(
                'title' => $this->translator->trans('datatables.actions.title'),
                'actions' => array(
                    array(
                        'route' => 'shrubs_show',
                        'route_parameters' => array(
                            'id' => 'id'
                        ),
                        'label' => $this->translator->trans('datatables.actions.show'),
                        'icon' => 'glyphicon glyphicon-eye-open',
                        'attributes' => array(
                            'rel' => 'tooltip',
                            'title' => $this->translator->trans('datatables.actions.show'),
                            'class' => 'btn btn-primary btn-xs',
                            'role' => 'button'
                        ),
                    ),
                    array(
                        'route' => 'shrubs_edit',
                        'route_parameters' => array(
                            'id' => 'id'
                        ),
                        'label' => $this->translator->trans('datatables.actions.edit'),
                        'icon' => 'glyphicon glyphicon-edit',
                        'attributes' => array(
                            'rel' => 'tooltip',
                            'title' => $this->translator->trans('datatables.actions.edit'),
                            'class' => 'btn btn-primary btn-xs',
                            'role' => 'button'
                        ),
                    )
                )
            ))
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function getEntity()
    {
        return 'AppBundle\Entity\Shrubs';
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'shrubs_datatable';
    }
}

通常,此行
$datatable=$em->getRepository('AppBundle:bushs')->findAll()将为您获取所有数据。在模板中,可以使用
{{dump(datatable)}}
查看datatable中的内容。对于新手来说,这个例子似乎有点复杂,首先应该遵循一个简单的教程;并获得错误类型错误:传递给Sg\DataTableBundle\Twig\DataTableTwigeExtension::datatableRender()的参数2必须是Sg\DataTableBundle\Datatable\View\AbstractDatatableView的实例,数组给定,在第54行的C:\my_project\u name\var\cache\dev\Twig\64\64EBBB790B43176FAA29FE2180F4E177EE69B760937E685BA3396EED8F.php中调用{{datatable_render(datatable)}
是一个自定义的细枝渲染函数。您可以像这样对它进行注释{code>{datatable_render(datatable)}
并转储数据,正如我在第一个注释中所解释的。这就是为什么我还说,如果您还不熟悉框架,那么这个示例有点复杂。array:718[▼ 0=>灌木{#674▶} 1=>灌木{#676▶} 2=>灌木{#677▶} 3=>灌木{#678▶} 4=>灌木{#679▶} 5=>灌木{#680▶} 6=>灌木{#681▶} 7=>灌木{#682▶} 8=>灌木{#683▶} 9=>灌木{#684▶} 10=>灌木{#685▶} etc..是来自转储数据表的内容现在您可以根据需要格式化数据。例如,您可以尝试
{%for datatable%}{{data.name}}
{%endfor%}
。无论如何,如果您正确获取数据,您只需了解自定义渲染函数
{datatable_render(datatable)}
的工作原理
    <?php

namespace AppBundle\Entity;

/**
 * Shrubs
 */
class Shrubs
{
    /**
     * @var string
     */
    private $botanicalname;

    /**
     * @var string
     */
    private $commonname;

    /**
     * @var string
     */
    private $borderlinehardy;

    /**
     * @var string
     */
    private $phpreference;

    /**
     * @var string
     */
    private $wetsoil;

    /**
     * @var string
     */
    private $moistsoil;

    /**
     * @var string
     */
    private $peatysoil;

    /**
     * @var string
     */
    private $welldrainedsoil;

    /**
     * @var string
     */
    private $drought;

    /**
     * @var string
     */
    private $claysoil;

    /**
     * @var string
     */
    private $sandysoil;

    /**
     * @var string
     */
    private $loamsoil;

    /**
     * @var string
     */
    private $infertilesoil;

    /**
     * @var string
     */
    private $richsoil;

    /**
     * @var string
     */
    private $compactedsoil;

    /**
     * @var string
     */
    private $cityconditions;

    /**
     * @var string
     */
    private $pollution;

    /**
     * @var string
     */
    private $salt;

    /**
     * @var string
     */
    private $windy;

    /**
     * @var string
     */
    private $shade;

    /**
     * @var string
     */
    private $partshade;

    /**
     * @var string
     */
    private $fullsun;

    /**
     * @var string
     */
    private $pestproblem;

    /**
     * @var integer
     */
    private $id;


    /**
     * Set botanicalname
     *
     * @param string $botanicalname
     *
     * @return Shrubs
     */
    public function setBotanicalname($botanicalname)
    {
        $this->botanicalname = $botanicalname;

        return $this;
    }

    /**
     * Get botanicalname
     *
     * @return string
     */
    public function getBotanicalname()
    {
        return $this->botanicalname;
    }

    /**
     * Set commonname
     *
     * @param string $commonname
     *
     * @return Shrubs
     */
    public function setCommonname($commonname)
    {
        $this->commonname = $commonname;

        return $this;
    }

    /**
     * Get commonname
     *
     * @return string
     */
    public function getCommonname()
    {
        return $this->commonname;
    }

    /**
     * Set borderlinehardy
     *
     * @param string $borderlinehardy
     *
     * @return Shrubs
     */
    public function setBorderlinehardy($borderlinehardy)
    {
        $this->borderlinehardy = $borderlinehardy;

        return $this;
    }

    /**
     * Get borderlinehardy
     *
     * @return string
     */
    public function getBorderlinehardy()
    {
        return $this->borderlinehardy;
    }

    /**
     * Set phpreference
     *
     * @param string $phpreference
     *
     * @return Shrubs
     */
    public function setPhpreference($phpreference)
    {
        $this->phpreference = $phpreference;

        return $this;
    }

    /**
     * Get phpreference
     *
     * @return string
     */
    public function getPhpreference()
    {
        return $this->phpreference;
    }

    /**
     * Set wetsoil
     *
     * @param string $wetsoil
     *
     * @return Shrubs
     */
    public function setWetsoil($wetsoil)
    {
        $this->wetsoil = $wetsoil;

        return $this;
    }

    /**
     * Get wetsoil
     *
     * @return string
     */
    public function getWetsoil()
    {
        return $this->wetsoil;
    }

    /**
     * Set moistsoil
     *
     * @param string $moistsoil
     *
     * @return Shrubs
     */
    public function setMoistsoil($moistsoil)
    {
        $this->moistsoil = $moistsoil;

        return $this;
    }

    /**
     * Get moistsoil
     *
     * @return string
     */
    public function getMoistsoil()
    {
        return $this->moistsoil;
    }

    /**
     * Set peatysoil
     *
     * @param string $peatysoil
     *
     * @return Shrubs
     */
    public function setPeatysoil($peatysoil)
    {
        $this->peatysoil = $peatysoil;

        return $this;
    }

    /**
     * Get peatysoil
     *
     * @return string
     */
    public function getPeatysoil()
    {
        return $this->peatysoil;
    }

    /**
     * Set welldrainedsoil
     *
     * @param string $welldrainedsoil
     *
     * @return Shrubs
     */
    public function setWelldrainedsoil($welldrainedsoil)
    {
        $this->welldrainedsoil = $welldrainedsoil;

        return $this;
    }

    /**
     * Get welldrainedsoil
     *
     * @return string
     */
    public function getWelldrainedsoil()
    {
        return $this->welldrainedsoil;
    }

    /**
     * Set drought
     *
     * @param string $drought
     *
     * @return Shrubs
     */
    public function setDrought($drought)
    {
        $this->drought = $drought;

        return $this;
    }

    /**
     * Get drought
     *
     * @return string
     */
    public function getDrought()
    {
        return $this->drought;
    }

    /**
     * Set claysoil
     *
     * @param string $claysoil
     *
     * @return Shrubs
     */
    public function setClaysoil($claysoil)
    {
        $this->claysoil = $claysoil;

        return $this;
    }

    /**
     * Get claysoil
     *
     * @return string
     */
    public function getClaysoil()
    {
        return $this->claysoil;
    }

    /**
     * Set sandysoil
     *
     * @param string $sandysoil
     *
     * @return Shrubs
     */
    public function setSandysoil($sandysoil)
    {
        $this->sandysoil = $sandysoil;

        return $this;
    }

    /**
     * Get sandysoil
     *
     * @return string
     */
    public function getSandysoil()
    {
        return $this->sandysoil;
    }

    /**
     * Set loamsoil
     *
     * @param string $loamsoil
     *
     * @return Shrubs
     */
    public function setLoamsoil($loamsoil)
    {
        $this->loamsoil = $loamsoil;

        return $this;
    }

    /**
     * Get loamsoil
     *
     * @return string
     */
    public function getLoamsoil()
    {
        return $this->loamsoil;
    }

    /**
     * Set infertilesoil
     *
     * @param string $infertilesoil
     *
     * @return Shrubs
     */
    public function setInfertilesoil($infertilesoil)
    {
        $this->infertilesoil = $infertilesoil;

        return $this;
    }

    /**
     * Get infertilesoil
     *
     * @return string
     */
    public function getInfertilesoil()
    {
        return $this->infertilesoil;
    }

    /**
     * Set richsoil
     *
     * @param string $richsoil
     *
     * @return Shrubs
     */
    public function setRichsoil($richsoil)
    {
        $this->richsoil = $richsoil;

        return $this;
    }

    /**
     * Get richsoil
     *
     * @return string
     */
    public function getRichsoil()
    {
        return $this->richsoil;
    }

    /**
     * Set compactedsoil
     *
     * @param string $compactedsoil
     *
     * @return Shrubs
     */
    public function setCompactedsoil($compactedsoil)
    {
        $this->compactedsoil = $compactedsoil;

        return $this;
    }

    /**
     * Get compactedsoil
     *
     * @return string
     */
    public function getCompactedsoil()
    {
        return $this->compactedsoil;
    }

    /**
     * Set cityconditions
     *
     * @param string $cityconditions
     *
     * @return Shrubs
     */
    public function setCityconditions($cityconditions)
    {
        $this->cityconditions = $cityconditions;

        return $this;
    }

    /**
     * Get cityconditions
     *
     * @return string
     */
    public function getCityconditions()
    {
        return $this->cityconditions;
    }

    /**
     * Set pollution
     *
     * @param string $pollution
     *
     * @return Shrubs
     */
    public function setPollution($pollution)
    {
        $this->pollution = $pollution;

        return $this;
    }

    /**
     * Get pollution
     *
     * @return string
     */
    public function getPollution()
    {
        return $this->pollution;
    }

    /**
     * Set salt
     *
     * @param string $salt
     *
     * @return Shrubs
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;

        return $this;
    }

    /**
     * Get salt
     *
     * @return string
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * Set windy
     *
     * @param string $windy
     *
     * @return Shrubs
     */
    public function setWindy($windy)
    {
        $this->windy = $windy;

        return $this;
    }

    /**
     * Get windy
     *
     * @return string
     */
    public function getWindy()
    {
        return $this->windy;
    }

    /**
     * Set shade
     *
     * @param string $shade
     *
     * @return Shrubs
     */
    public function setShade($shade)
    {
        $this->shade = $shade;

        return $this;
    }

    /**
     * Get shade
     *
     * @return string
     */
    public function getShade()
    {
        return $this->shade;
    }

    /**
     * Set partshade
     *
     * @param string $partshade
     *
     * @return Shrubs
     */
    public function setPartshade($partshade)
    {
        $this->partshade = $partshade;

        return $this;
    }

    /**
     * Get partshade
     *
     * @return string
     */
    public function getPartshade()
    {
        return $this->partshade;
    }

    /**
     * Set fullsun
     *
     * @param string $fullsun
     *
     * @return Shrubs
     */
    public function setFullsun($fullsun)
    {
        $this->fullsun = $fullsun;

        return $this;
    }

    /**
     * Get fullsun
     *
     * @return string
     */
    public function getFullsun()
    {
        return $this->fullsun;
    }

    /**
     * Set pestproblem
     *
     * @param string $pestproblem
     *
     * @return Shrubs
     */
    public function setPestproblem($pestproblem)
    {
        $this->pestproblem = $pestproblem;

        return $this;
    }

    /**
     * Get pestproblem
     *
     * @return string
     */
    public function getPestproblem()
    {
        return $this->pestproblem;
    }

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