这个代码来从我的数据库中的所有用户名和打印的网页上,但我从这一行symfony的通知:未定义指数:用户名
echo $row['username'];
有什么错得到这个错误?
<?php
namespace test\stuffBundle\Controller;
use tuto\testBundle\Entity\Users;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\HttpFoundation\Session\Session;
use Ratchet\WebSocket\WsServerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
public function indexAction()
{
$conn = $this->get('database_connection');
while ($row = $conn->fetchAll('SELECT username FROM user')) {
echo $row['username'];
}
return $this->render('teststuffBundle:Default:index.html.twig');
}
}
我的实体的用户文件
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="`user`")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*@var integer
*
* @ORM\Column(name="MatchP",type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $MatchP;
public function getMatchP()
{
return $this->MatchP;
}
public function setMatchP($MatchP)
{
$this->MatchP = $MatchP;
}
public function __construct()
{
parent::__construct();
}
}
我print_r($row);
输出
Array ([0] => Array ([username] => shar) [1] => Array ([username] => koko)) Array ([0] => Array ([username] => shar) [1]
=>阵列([用户名] =>科科))
'print_r($ row);' – nogad
您能否显示您的'user'实体代码? –
但是'User'实体没有'username'。所以你的查询将是空的。你的意思是不同的实体吗? –