I have a User Model:
use Cartalyst\Sentry\Users\Eloquent\User as SentryModel;
class User extends SentryModel {
public function raspberries() {
return $this->hasMany('Raspberry');
}
}
And a SentryModel:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class SentryModel extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* #return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
And a Rasberry Model:
class Raspberry extends Eloquent{
# Relations
public function user(){
return $this->belongsTo('User');
}
}
And now I want select all Rasberries from my logged user:
$data = Sentry::getUser()->raspberries()->paginate(10);
But this doesn't work, I got error Message:
BadMethodCallException
Call to undefined method
Illuminate\Database\Query\Builder::raspberries()
Why?
Related
When trying to get the default repository class for my entity Lesson I keep getting the following error:
Attempted to load class "LessonRepository" from namespace "DrumLessonBookingApp\DrumLessonBookingBundle\Repository".
Did you forget a "use" statement for another namespace?
I use the exact same method as a User entity which works perfectly fine.
The code for my controller is:
namespace DrumLessonBookingApp\DrumLessonBookingBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use DrumLessonBookingApp\DrumLessonBookingBundle\Entity\User;
use DrumLessonBookingApp\DrumLessonBookingBundle\Entity\Lesson;
use DrumLessonBookingApp\DrumLessonBookingBundle\Repository;
class LoginController extends Controller
{
public function displayAction()
{
return $this->render('DrumLessonBookingAppDrumLessonBookingBundle:Default:login.html.twig');
}
public function processloginAction(Request $request)
{
$doctrine = $this->getDoctrine()->getManager();
$email = $request->get('email');
$pwd = $request->get('pwd');
$user = $doctrine->getRepository('DrumLessonBookingAppDrumLessonBookingBundle:User')->findOneBy(array('email' => $email,'password' => $pwd));
if($user->getAdministrator() == 1)
{
$session = $this->get('session');
$session->set('loggedin', true);
$lessons = $doctrine->getRepository('DrumLessonBookingAppDrumLessonBookingBundle:Lesson')->findAll();
return $this->render('DrumLessonBookingAppDrumLessonBookingBundle:Default:dashboard.html.twig', array("lessons" => $lessons));
}
else {
return new Response('doctrine not working');
}
}
}
I am also having difficulty to generate a custom Respository class using doctrine so tried creating one myself for the User entity but symfny doesn't pick it up or recognise the custom method. See below:
namespace DrumLessonBookingApp\DrumLessonBookingBundle\Repository;
use Doctrine\ORM\EntityRepository;
use DrumLessonBookingApp\DrumLessonBookingBundle\Entity\User;
class UserRepository extends EntityRepository
{
public function loginUser($email, $password)
{
$entityM = $this->getEntityManager();
$dql = $entityM->createQuery(
'SELECT u FROM DrumLessonBookingAppDrumLessonBookingBundle:User u
WHERE u.email = :email
AND WHERE u.password = :pwd');
$dql->setParameter('email', $email);
$dql->setParameter('pwd', $password);
$user = $query->getResult();
return $user;
}
}
I have searched similar questions but cannot find a solution, someone please help!
My Lesson Entity
namespace DrumLessonBookingApp\DrumLessonBookingBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="lessons")
* #ORM\Entity(repositoryClass="DrumLessonBookingApp\DrumLessonBookingBundle\Repository\LessonRepository")
* */
class Lesson {
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* */
private $id;
/**
* #ORM\Column(type="date")
* */
private $date;
/**
* #ORM\Column(type="time")
* */
private $time;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="lessons")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* */
private $user;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set date
*
* #param \DateTime $date
*
* #return Lesson
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set time
*
* #param \DateTime $time
*
* #return Lesson
*/
public function setTime($time)
{
$this->time = $time;
return $this;
}
/**
* Get time
*
* #return \DateTime
*/
public function getTime()
{
return $this->time;
}
/**
* Set User
*
* #param object
* #return Lesson
* */
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/**
* Set User
*
*
* #return User
* */
public function getUser($user)
{
return $this->user;
}
}
There is nothing in my custom Lesson Repository but surely it should still find the methods such as findAll() etc from the entityRepository it extends
seems you have multiple questions for one answer
you need to reference the repositoryClass it in your Entity-class :
/**
* USER
*
* #ORM\Table(name="users")
* #ORM\Entity(repositoryClass="DrumLessonBookingApp\DrumLessonBookingBundle\Repository\UserRepository")
*/
class User
{
i guess you missing the same at your Lesson Entity.
You dont have to add a "use" statement then
I have a simple car model with one attribute:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
/*
* TODO:
* Property Fotos ausfüllen
*/
class Car extends Model
{
//Tablenname
protected $table = 'cars';
protected $fillable = ['title'];
public $timestamps = false;
/**
* #var string
*/
protected $title = NULL;
/**
* #var integer
*/
protected $imagesId = NULL;
/**
*
* #return string
*/
public function getTitle()
{
return $this->attributes['title'];
}
/**
*
* #param string $title
*
* #return void
*/
public function setTitle($title)
{
$this->attributes['title'] = $title;
}
}
This is my store function from the controller:
<?php
namespace App\Http\Controllers;
use App\Car;
class CarController extends Controller
{
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$car = new Car();
// $car->setTitle($request->title);
$car->setTitle('stackoverflow');
$car->save();
...
}
}
However, this is how the entry in the database looks like:
Title is always zero! I also tried it with other models, same.
Check field datatype.if you want to store string you have to change your 'title' field datatype to varchar.
hope it works.
I am trying to send activation link to registered user with the help of laravel. I have made some changes in User.php but
"Declaration of User::setRememberToken() must be compatible with
Illuminate\Auth\UserInterface::setRememberToken($value)"
this error is coming.
my User.php is as follows:
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
//use UserTrait, RemindableTrait;
protected $fillable =array('email','username','password','password_temp','code','active');
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password');
/**
* get the identifier for user
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* get the password for user
*
* #return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* get the email add where password is sent
*
* #return string
*/
public function getRemainderEmail()
{
return $this->email;
}
public function getRememberToken(){}
public function setRememberToken(){}
public function getReminderEmail(){}
}
If you look at the docs for setRememberToken, you can see that it has a signature of void setRememberToken(string $value). So, your code change
public function setRememberToken(){}
to
public function setRememberToken($value){}
I'm trying to set up my User entity to use roles, and following the documentation at http://symfony.com/doc/current/cookbook/security/entity_provider.html
Users work fine, and if I hardcode the $roles value everything works as expected, log in/out is good, etc. But, if I try and retrieve the roles through a many-to-many relationship as outlined in the documentation I get back null.
I should also mention that after creating the entities when I ran 'php app/console doctrine:schema:update --force' it created the role table, but not the 'user_role' table as it said it would. I went ahead and created it manually and entered a row for the user I was testing with, but that was my first clue something wasn't working. It's really frustrating because I followed the documentation and it looks like it should work.
The error I get back while trying to log-in is:
FatalErrorException: Error: Call to a member function toArray() on a non-object
Which points to return $this->roles->toArray() in the user entity.
My User Entity (the relevant bits):
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="ACME\MyBundle\Entity\UserRepository")
*
*/
class User implements UserInterface, \Serializable
{
...
/**
* #ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*
*/
private $roles;
...
/**
* Constructor
*/
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getRoles()
{
return $this->roles->toArray();
}
...
}
My Role Entity:
use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table(name="role")
* #ORM\Entity()
*/
class Role implements RoleInterface
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id()
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="name", type="string", length=30)
*/
private $name;
/**
* #ORM\Column(name="role", type="string", length=20, unique=true)
*/
private $role;
/**
* #ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* #see RoleInterface
*/
public function getRole()
{
return $this->role;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Role
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set role
*
* #param string $role
* #return Role
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
}
Does anybody see a problem in my code or have experience with this same issue? I'm stuck at the moment.
In your entity Role you have
* #ORM\Table(name="role")
change it to
* #ORM\Table(name="user_role")
because your table name is user_role not role
I had the same probleme and i removed the toArray methode
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="ACME\MyBundle\Entity\UserRepository")
*
*/
class User implements UserInterface, \Serializable
{
...
/**
* #ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*
*/
private $roles;
...
/**
* Constructor
*/
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getRoles()
{
return $this->roles;//return $this->roles->toArray();
}
...
}
First of all: the error I'm getting is: Entities passed to the choice field must be managed
I have these entities:
- user (belongs to one or many teams)
- team (has one or 2 users)
- challenge (has 2 teams)
I'd like to build a ChallengeType form where a user can fill in the two users for the two teams and create the challenge. I think I need an embedded form here.
I've made a TeamType Form class: (I would expect to get a select box from this, where all users are listed)
<?php
namespace Tennisconnect\DashboardBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class TeamType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('players', 'entity', array(
'class' => 'TennisconnectUserBundle:User',
'multiple' => true
));
}
public function getName()
{
return 'team';
}
public function getDefaultOptions(array $options)
{
return array('data_class' => 'Tennisconnect\DashboardBundle\Entity\Team');
}
}
This is the ChallengeType form class:
<?php
namespace Tennisconnect\DashboardBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ChallengeType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('teams', 'collection', array('type' => new TeamType()));
}
public function getName()
{
return 'challenge';
}
public function getDefaultOptions(array $options)
{
return array('data_class' => 'Tennisconnect\DashboardBundle\Entity\Challenge');
}
}
Challenge entity:
namespace Tennisconnect\DashboardBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Tennisconnect\DashboardBundle\Entity\Team;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="challenge")
*/
class Challenge
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="Team", mappedBy="teams")
*/
protected $teams;
public function __construct()
{
$this->teams = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add teams
*
* #param Tennisconnect\DashboardBundle\Entity\Team $teams
*/
public function addTeam(Team $teams)
{
$this->teams[] = $teams;
}
/**
* Get teams
*
* #return Doctrine\Common\Collections\Collection
*/
public function getTeams()
{
return $this->teams;
}
}
Team entity:
namespace Tennisconnect\DashboardBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Tennisconnect\UserBundle\Entity\User;
use Tennisconnect\DashboardBundle\Entity\Challenge;
use Tennisconnect\DashboardBundle\Entity\Match;
/**
* #ORM\Entity
* #ORM\Table(name="team")
*/
class Team
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="Tennisconnect\UserBundle\Entity\User", mappedBy="teams")
*/
protected $players;
/**
* #ORM\ManyToMany(targetEntity="challenge", inversedBy="teams", cascade= {"persist"})
*/
protected $challenges;
/**
* #ORM\ManyToMany(targetEntity="Match", inversedBy="teams")
*/
protected $matches;
public function __construct()
{
$this->players = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add players
*
* #param Tennisconnect\UserBundle\Entity\User $players
*/
public function addUser(User $players)
{
$this->players[] = $players;
}
/**
* Get players
*
* #return Doctrine\Common\Collections\Collection
*/
public function getPlayers()
{
return $this->players;
}
/**
* Add matches
*
* #param Tennisconnect\DashboardBundle\Entity\Match $matches
*/
public function addMatch(Match $matches)
{
$this->matches[] = $matches;
}
/**
* Get matches
*
* #return Doctrine\Common\Collections\Collection
*/
public function getMatches()
{
return $this->matches;
}
/**
* Add challenges
*
* #param Tennisconnect\DashboardBundle\Entity\challenge $challenges
*/
public function addchallenge(challenge $challenges)
{
$this->challenges[] = $challenges;
}
/**
* Get challenges
*
* #return Doctrine\Common\Collections\Collection
*/
public function getChallenges()
{
return $this->challenges;
}
}
Challenge controller:
class ChallengeController extends Controller
{
public function newAction()
{
$challenge = new Challenge();
$form = $this->createForm(new ChallengeType(), $challenge);
return $this->render('TennisconnectDashboardBundle:Challenge:new.html.twig', array('form' => $form->createView()));
}
}
You've created forms that are displaying a ManyToMany collection; set the multiple option in your formbuilder for those widgets to true (it defaults false, which fundamentally conflicts with a ToMany relationship).
If you have the error Entities passed to the choice field must be managed. Maybe persist them in the entity manager? with a ManyToMany relationship between 2 entities, when using a form type, it may come from your entity constructor :
If your form is "TeamType", try to remove the ArrayCollection initialization of your "Team" entity.
Your Team class become :
namespace Tennisconnect\DashboardBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Tennisconnect\UserBundle\Entity\User;
use Tennisconnect\DashboardBundle\Entity\Challenge;
use Tennisconnect\DashboardBundle\Entity\Match;
/**
* #ORM\Entity
* #ORM\Table(name="team")
*/
class Team
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="Tennisconnect\UserBundle\Entity\User", mappedBy="teams")
*/
protected $players;
/**
* #ORM\ManyToMany(targetEntity="challenge", inversedBy="teams", cascade= {"persist"})
*/
protected $challenges;
/**
* #ORM\ManyToMany(targetEntity="Match", inversedBy="teams")
*/
protected $matches;
public function __construct()
{
// REMOVE $this->players = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add players
*
* #param Tennisconnect\UserBundle\Entity\User $players
*/
public function addUser(User $players)
{
$this->players[] = $players;
}
/**
* Get players
*
* #return Doctrine\Common\Collections\Collection
*/
public function getPlayers()
{
return $this->players;
}
/**
* Add matches
*
* #param Tennisconnect\DashboardBundle\Entity\Match $matches
*/
public function addMatch(Match $matches)
{
$this->matches[] = $matches;
}
/**
* Get matches
*
* #return Doctrine\Common\Collections\Collection
*/
public function getMatches()
{
return $this->matches;
}
/**
* Add challenges
*
* #param Tennisconnect\DashboardBundle\Entity\challenge $challenges
*/
public function addchallenge(challenge $challenges)
{
$this->challenges[] = $challenges;
}
/**
* Get challenges
*
* #return Doctrine\Common\Collections\Collection
*/
public function getChallenges()
{
return $this->challenges;
}
}
The problem is solved.
I had to add the "allow_add" option to my collection in the ChallengeType class.
The challenge controller class needed some editing too. I added 2 teams to the Challenge object before passing it through to the form.