Error While Creating the schema for doctroin - laravel-5

I have tried to create the database schema by running the artisan command through CLI and got the following error:
The annotation "#Doctrine\ORM\Mapping\Names" in property App\Entity\Vouchers::$names does not exist, or could not be auto-loaded.
Following the Code for my Entity.
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="vouchers")
**/
class Vouchers
{
/**
* #var integer $id
* #ORM\Column(name="id", type="integer", unique=true, nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*
*/
private $id;
/**
* #var string $names
* #ORM\Column(name="names", type="string", unique=false, nullable=false)
* #ORM\Names
*
*/
private $names;
/**
* #var string $amount
* #ORM\Column(name="amount", type="string", unique=false, nullable=false)
* #ORM\Amount
*
*/
private $amount;
//Getter And Setters
//ID
public function getId()
{
return $this->id;
}
//name
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
//amount
public function getAmount()
{
return $this->amount;
}
public function setAmount($amount)
{
$this->amount = $amount;
}
}
Please check and let me know if there is any mistake i have made in the above code and how it can be fixed. I have tried many other solution but still i am not able to figure this out.

Fixed it by replacing the name section with the following
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=150, nullable=false)
*/
private $name;

Related

Doctrine in laravel, getting all user's roles

I have a simple project in laravel 5.4 with Doctrine 2.0. I have three tables: users, roles, user_roles. Screenshot from tables schemas from HEIDISQL I show below:
users:
roles:
user_roles:
I have three Entities classes for each table of course:
<?php
namespace TodoList\Http\Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="users")
*/
class User
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*
*/
private $id;
/**
*
* #ORM\Column(type="string")
*
*/
private $name;
/**
*
* #ORM\Column(type="string")
*
*/
private $email;
/**
* #ORM\OneToMany(targetEntity="Task", mappedBy="user", cascade={"persist"})
* #var ArrayCollection|Task[]
*/
private $tasks;
/**
* #ORM\OneToMany(targetEntity="UserRole", mappedBy="user")
*/
protected $user_roles;
/**
* User constructor
* #param #name
* #param #email
* #param $password
*/
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
$this->tasks = new ArrayCollection();
$this->user_roles = new ArrayCollection();
}
/**
*
* #return mixed
*
*
*/
public function getName()
{
return $this->name;
}
/**
* #return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* #return mixed
*/
public function getTasks()
{
return $this->tasks;
}
public function addTask(Task $task)
{
if(!$this->tasks->contains($task)) {
$task->setUser($this);
$this->tasks->add($task);
}
}
public function getId(){
return $this->id;
}
public function getUserRoles(){
return $this->user_roles;
}
}
<?php
namespace TodoList\Http\Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="roles")
*
*/
class Role{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string")
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="description", type="text")
*/
private $description;
/**
* #ORM\OneToMany(targetEntity="UserRole", mappedBy="role",cascade={"persist"})
*/
protected $user_roles;
public function getId(){
return $this->id;
}
public function getName(){
return $this->name;
}
public function getDescription(){
return $this->description;
}
}
<?php
namespace TodoList\Http\Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="user_roles")
*/
class UserRole
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* #ORM\ManyToOne(targetEntity="User", inversedBy="user_roles")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*
*/
private $user;
/**
*
* #ORM\ManyToOne(targetEntity="Role", inversedBy="user_roles")
* #ORM\JoinColumn(name="role_id", referencedColumnName="id")
*/
private $role;
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \NVC\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set recipe
*
* #param \NVC\RecipeBundle\Entity\Recipe $recipe
* #return UserRecipeAssociation
*/
public function setRole(Role $role)
{
$this->role = $role;
return $this;
}
/**
* Get recipe
*
* #return \NVC\RecipeBundle\Entity\Recipe
*/
public function getRole()
{
return $this->role;
}
}
I have a problem when I'm trying retrieve roles of particular user by doctrine Entity Managar. I'm trying to do that in that way:
public function showUserRoles(EntityManagerInterface $em){
$userRoles = $em->getRepository('\TodoList\Http\Entities\UserRole');
$userRoles->findBy(['user' => 2]);
//count($userRole);
}
There is an error from method findBy
Error message is:
(1/1) FatalErrorException
Doctrine\Common\Proxy\AbstractProxyFactory::getProxyDefinition():
Failed opening required
'C:\xampp\htdocs\Project\storage\proxies__CG__TodoListHttpEntitiesRole.php'
(include_path='C:\xampp\php\PEAR')
Is anything wrong with my Entities? I don't know how could I solve that problem. Could someone help me with that?
I would be very greateful
Best regards ;)
You need to set folder C:\xampp\htdocs\Project\storage\proxies\ as writable and then you need to generate proxies by console command:
doctrine:generate:proxies

Symfony: Only one file entity and different validation rules for each association?

In my current project, I decided to create only one translatable File entity and reuse it for all image/document properties I have. For the translations, I uses Knp Doctrine Behaviors Translatable. So here is the code.
The file class:
class File
{
use ORMBehaviors\Translatable\Translatable;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __toString()
{
return (string)$this->id;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
The translatable file class:
class FileTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
public $name;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
/**
* #Assert\File()
*/
private $file;
/*
* Non tracked parameter
*/
public $folder;
/**
* Set name.
*
* #param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name.
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set path.
*
* #param string $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* Get path.
*
* #return string
*/
public function getPath()
{
return $this->path;
}
/**
* Sets file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Set folder
*
* #param string $folder
*
* #return File
*/
public function setFolder($folder)
{
$this->folder = $folder;
return $this;
}
/**
* Get folder
*
* #return File
*/
public function getFolder()
{
return $this->folder;
}
}
And then an example of how it's used in another entity (User) to create an image property:
class User
{
/**
* #ORM\OneToOne(targetEntity="File", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="image_id", referencedColumnName="id", nullable=true)
* #Assert\Valid()
*/
private $image;
}
Nothing new. I just followed Symfony/Knp documentation and it works fine. However, now I want to add different validation rules each time I create a new property like $image for different entities. What is the best strategy here?
Each time I try to add a validation rule related to file in the $image property, for instance, it says it cannot find a file.
you can have specific validator for each entity:
/**
* vérification des constraintes
* #Assert\Callback
*/
public function validate(ExecutionContextInterface $context)
{
var_dump($this->image);// do your check here
}
For those who have the same problem, I finally did what #sylvain said and I created my custom validators:
http://symfony.com/doc/current/cookbook/validation/custom_constraint.html
This is a great tutorial, btw:
https://knpuniversity.com/screencast/question-answer-day/custom-validation-property-path
It works fine. However, I still think the #valid constraint should have worked and I don't understand why it didn't.

Doctrine generate Models. Metadata Configuration

I generated Entities with Doctrine's generating method inside Doctrine.php from CodeIgniter Controller:
function generate_classes(){
$this->em->getConfiguration()
->setMetadataDriverImpl(
new DatabaseDriver(
$this->em->getConnection()->getSchemaManager()
)
);
$cmf = new DisconnectedClassMetadataFactory();
$cmf->setEntityManager($this->em);
$metadata = $cmf->getAllMetadata();
$generator = new EntityGenerator();
$generator->setUpdateEntityIfExists(true);
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, APPPATH."models/Entities");
}
The generated Entity had this syntax:
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*
* #ORM\Table(name="users")
* #ORM\Entityz
*/
class Users
{
/**
* #var integer
*
* #ORM\Column(name="idusers", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idusers;
/**
* #var string
*
* #ORM\Column(name="first_name", type="string", length=45, nullable=true)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="last_name", type="string", length=45, nullable=true)
*/
Now calling these Entities form my model didn't work. The model didn't find the entity. After manually changing the syntax to the following code example, I got it to work:
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*
* #Entity #Table(name="users")
*/
class Users
{
/**
* #var integer
*
* #Column(name="idusers", type="integer", nullable=false)
* #Id
*
*/
private $idusers;
/**
*
* #GeneratedValue
* #Column(name="first_name", type="string", length=45, nullable=true)
*/
private $firstName;
And now everything works!
I am not going to manually change every Entity.
How can I configure Doctrine to produce Entity's with the correct syntax. As used in their tutorials as well? http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/getting-started.html
Noob question. Please help!
Found the solution:
changed in application/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/EntityGenerator.php the constructor from:
public function __construct()
{
if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
$this->annotationsPrefix = 'ORM\\';
}
}
To:
public function __construct()
{
if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
$this->annotationsPrefix = '';
}
}

integrating doctrine 2 in codeignitier 2

Currently i am trying to integrate doctrine 2 in codeigniter 2 and i am doing all this by following the below tutorial.
http://www.joelverhagen.com/blog/2011/05/setting-up-codeigniter-2-with-doctrine-2-the-right-way/
According to the tutorial i had
1)Doctrine 2 in library
2)Doctrine.php file in library
3)doctrine-cli.php in root folder
4)yaml file inside applications/models/mappings and also entities and proxies inside the same location
According to the tutorial from the command prompt i had made the entities as
User entitiy
<?php
namespace Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* Entities\User
*/
class User
{
/**
* #var integer $id
*/
private $id;
/**
* #var string $password
*/
private $password;
/**
* #var string $firstName
*/
private $firstName;
/**
* #var string $lastName
*/
private $lastName;
/**
* #var string $email
*/
private $email;
/**
* #var string $website
*/
private $website;
/**
* #var datetime $created
*/
private $created;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* 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 firstName
*
* #param string $firstName
* #return User
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* #param string $lastName
* #return User
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set email
*
* #param string $email
* #return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set website
*
* #param string $website
* #return User
*/
public function setWebsite($website)
{
$this->website = $website;
return $this;
}
/**
* Get website
*
* #return string
*/
public function getWebsite()
{
return $this->website;
}
/**
* Set created
*
* #param datetime $created
* #return User
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return datetime
*/
public function getCreated()
{
return $this->created;
}
}?>
also the article entitiy as
<?php
namespace Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* Entities\Article
*/
class Article
{
/**
* #var integer $id
*/
private $id;
/**
* #var string $title
*/
private $title;
/**
* #var text $content
*/
private $content;
/**
* #var datetime $created
*/
private $created;
/**
* #var Entities\User
*/
private $user;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Article
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set content
*
* #param text $content
* #return Article
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return text
*/
public function getContent()
{
return $this->content;
}
/**
* Set created
*
* #param datetime $created
* #return Article
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return datetime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set user
*
* #param Entities\User $user
* #return Article
*/
public function setUser(\Entities\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return Entities\User
*/
public function getUser()
{
return $this->user;
}
}
i had done every thing as explained in that tutorial but when i try to insert the value in database table i got the error as
Fatal error: Class 'Entities' not found in D:\xampp\htdocs\ci_doctrine\application\controllers\welcome.php on line 32
while i use welcome controller to insert the data and my controller is
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function __construct() {
parent::__construct();
// $this->load->library('Doctrine.php');
//$this->em = $this->doctrine->em;
}
public function index()
{
// create a new user object
$user = new Entities/User;
$user->setFirstName('Joel');
$user->setLastName('Verhagen');
$user->setPassword(md5('Emma Watson'));
$user->setEmail('joel#joelverhagen.com');
$user->setWebsite('http://www.joelverhagen.com');
$user->setCreated(new DateTime());
// standard way in CodeIgniter to access a library in a controller: $this->library_name->member->memberFunction()
// save the object to database, so that it can obtain its ID
$this->doctrine->em->persist($user);
// create a new article object
$article = new Entities/Article;
$article->setTitle('Emma Watson is extremely talented, no?');
$article->setContent('By talented, I mean good at lookin\' good.');
$article->setCreated(new DateTime());
// the user object you pass must be persisted first!
$article->setUser($user);
// save the article object to the database
$this->doctrine->em->persist($article);
$this->doctrine->em->flush();
echo "<b>Success!</b>";
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
what is going on i can't figure out .... any suggestion and help..??
You don't use the fully qualified namespace but a relative one.
http://php.net/manual/en/language.namespaces.basics.php
That's why.
Try
$article = new \Entities\Article;
in your controller.
EDIT: Oh even better. You were trying to divide Entities with Article.
You were using / instead of \.

Symfony2/Doctrine2 One-To-Many same object twice

My current Client-Entity has an unloading and a loading Area, which are both ClientArea-Entities.
namespace ACME\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Sorien\DataGridBundle\Grid\Mapping as GRID;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* ACME\DemoBundle\Entity\Client
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="ACME\DemoBundle\Entity\ClientRepository")
*/
class Client
{
/**
* #ORM\OneToMany(targetEntity="ClientArea",mappedBy="client", cascade={"persist", "remove"})
*/
public $unloading_areas;
/**
* #ORM\OneToMany(targetEntity="ClientArea",mappedBy="client", cascade={"persist", "remove"})
*/
public $loading_areas;
}
class ClientArea
{
/**
* #ORM\ManyToOne(targetEntity="Client")
*/
public $client;
}
This does not work because client can only map 1 association.
How can i map the relation properly?
To create entity relations you need to have keys to use when joining tables. Your Client class should have an id key defined and you need to initialize collections, like this:
class Client
{
//....
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="ClientArea", mappedBy="client", cascade={"persist", "remove"})
*/
public $unloading_areas;
/**
* #ORM\OneToMany(targetEntity="ClientArea", mappedBy="client", cascade={"persist", "remove"})
*/
public $loading_areas;
public function __construct() {
// Initialize collections
$this->unloading_areas = new \Doctrine\Common\Collections\ArrayCollection();
$this->loading_areas = new \Doctrine\Common\Collections\ArrayCollection();
}
// ....
}
Your ClientArea class should then look something like this:
class ClientArea
{
// ....
/**
* #ORM\Column(name="client_id", type="int", nullable=false)
*/
private $clientId;
/**
* #ORM\ManyToOne(targetEntity="Client")
* #JoinColumn(name="client_id", referencedColumnName="id")
*/
public $client;
// ....
}
Now, those two entities should be mapped correctly.
To learn more about association mappings in Doctrine, read article here: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html
Hope this helps.

Resources