我收到错误'错误:调用未定义的方法Login \ LogBundle \ Entity \ User :: getToken()' 当试图从另一个实体apiuserdetails获取数据时。 user_id是apiuserdetails中的外键。我一直在关注示例http://symfony.com/doc/current/book/doctrine.html关系映射模型。我能够使用getRepository(“LoginLogBundle:UserApiProfile”)从用户实体获取信息,但如果按照他们的描述反向执行,我会得到错误。我在做映射时出错了但看不到它。非常感谢一些帮助谢谢。
控制器代码是
namespace Login\LogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Login\LogBundle\Entity\User;
use Login\LogBundle\Entity\UserApiDetails;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints as Assert;
class DefaultController extends Controller
{
public function userprofileAction(Request $request) {
if ($request->getMethod()=='POST') {
$name=$request->get('name');
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository("LoginLogBundle:User");
$user = $repository->findOneBy(array('username'=>$name));
return $this->render('LoginLogBundle:Default:userprofile.html.twig',array('token' => $user->getToken()));
}
}
else {
return $this->render('LoginLogBundle:Default:userprofile.html.twig');
}
}
}
实体 - 用户
namespace Login\LogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="Login\LogBundle\Entity\Repository\UserRepository")
* @ORM\Table(name="user")
* @ORM\HasLifecycleCallbacks()
*
* User
*/
class User{
/**
* @var string
*/
private $password;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $username;
/**
* @var integer
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="UserApiDetails", mappedBy="User")
*/
protected $userapidetails;
public function __construct()
{
$this->userapidetails = new ArrayCollection();
}
/**
* Set password
*
* @param string $password
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set name
*
* @param string $name
* @return User
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set username
*
* @param string $username
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
实体 - ApiUserDetails
namespace Login\LogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="Login\LogBundle\Entity\Repository \UserApiDetailsRepository")
* @ORM\Table(name="user_api_details")
* @ORM\HasLifecycleCallbacks()
*
* UserApiDetails
*/
class UserApiDetails{
/**
* @var string
*/
private $apiScheme;
/**
* @var string
*/
private $apiUrl;
/**
* @var string
*/
private $token;
/**
* @var string
*/
private $tokenSecret;
/**
* @var integer
*/
private $apiId;
/**
* @var \Login\LogBundle\Entity\User
*
* @ORM\ManyToOne(targetEntity="User",inversedBy="userapidetails")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* Set apiScheme
*
* @param string $apiScheme
* @return UserApiDetails
*/
public function setApiScheme($apiScheme)
{
$this->apiScheme = $apiScheme;
return $this;
}
/**
* Get apiScheme
*
* @return string
*/
public function getApiScheme()
{
return $this->apiScheme;
}
/**
* Set apiUrl
*
* @param string $apiUrl
* @return UserApiDetails
*/
public function setApiUrl($apiUrl)
{
$this->apiUrl = $apiUrl;
return $this;
}
/**
* Get apiUrl
*
* @return string
*/
public function getApiUrl()
{
return $this->apiUrl;
}
/**
* Set token
*
* @param string $token
* @return UserApiDetails
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
/**
* Get token
*
* @return string
*/
public function getToken()
{
return $this->token;
}
/**
* Set tokenSecret
*
* @param string $tokenSecret
* @return UserApiDetails
*/
public function setTokenSecret($tokenSecret)
{
$this->tokenSecret = $tokenSecret;
return $this;
}
/**
* Get tokenSecret
*
* @return string
*/
public function getTokenSecret()
{
return $this->tokenSecret;
}
/**
* Get apiId
*
* @return integer
*/
public function getApiId()
{
return $this->apiId;
}
/**
* Set user
*
* @param \Login\LogBundle\Entity\User $user
* @return UserApiDetails
*/
public function setUser(\Login\LogBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \Login\LogBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
答案 0 :(得分:0)
如果您收到\Login\LogBundle\Entity\User
,则表示您没有setToken
并在错误消息中说明。而且,由于您没有\Login\LogBundle\Entity\UserApiDetails
的导航属性,实际上,如果您朝着这个方向前进,那就会卡住。您可以使用User
中的导航属性,获取(单个或列表)UserApiDetails
,也可以使用UserApiDetails
并使用导航属性获取User
信息。在最后一种情况下,您只需在树枝{{ userapi.user.token }}