does not contain any mapped entities while generating getter and setter - symfony

i am tying to create one to one relation in symfony 2 using doctrine
here is my entity class
<?php
namespace Travel\HomeBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/** #ORM\Column(name="facebook_id", type="string", length=255, nullable=true) */
protected $facebook_id;
/** #ORM\Column(name="facebook_access_token", type="string", length=255, nullable=true) */
protected $facebook_access_token;
/**
#ORM\Column(name="travel_id", type="string", length=255, nullable=true)
#ORM\OneToOne(targetEntity="Travel\HomeBundle\Entity\Client")
#ORM\JoinColumn(name="travel_id", referencedColumnName="id")
*/
protected $travel_id;
/**
#ORM\Column(name="travel_access_token", type="string", length=255, nullable=true)
#ORM\OneToOne(targetEntity="Travel\HomeBundle\Entity\Client")
#ORM\JoinColumn(name="travel_access_token", referencedColumnName="secret")
*/
protected $travel_access_token;
public function __construct()
{
parent::__construct();
// your own logic
}
}
?>
after updating schema when i run following command to generate getter and setter methods
php app/console doctrine:generate:entities Travel/HomeBundle/Entity/User.php
i am getting following Runtime Error
Namespace "Travel\HomeBundle\Entity\User.php" does not contain any mapped
entities.

doctrine:generate:entities does not take file extensions. Just remove the .php.
The following are example of acceptable parameters for doctrine:generate:entities:
php app/console doctrine:generate:entities Travel/HomeBundle/Entity/User
Or
php app/console doctrine:generate:entities TravelHomeBundle:User

Related

FOSUserBundle and custom fields in user entity

I'm using FOSUserBundle with custom fields in my user class.
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
* #ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
*/
class User extends BaseUser {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255)
*
* #Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* #Assert\Length(min="3", max="255", groups={"Registration", "Profile"})
*/
protected $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Message", mappedBy="fromUser")
*/
protected $sentMessages;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Message", mappedBy="toUser")
*/
protected $receivedMessages;
/**
* #ORM\Column(type="date")
*/
protected $dateRegistered;
/**
* #ORM\Column(type="string", length=250, nullable=true)
*
*/
protected $banReason;
public function __construct() {
parent::__construct();
$this->dateRegistered = new \DateTime();
}
/* getters and setters */
}
I want to show a personal message to the locked users, filling $banReason field.
I found that error.user variable contains part of the user entity, but my field is set to null instead of it's real value.
Sorry, but i'm kinda noob in hooks and services. :-(
Have a nice day!

Abstract Class for Doctrine entity

I am trying to make an abstract class for my entities involving general fields like created_at and updated_at values:
<?php
namespace AppBundle;
use Doctrine\ORM;
abstract class Model {
/**
* #var \DateTime $created_at
*
* #ORM\Column(type="datetime")
*/
private $created_at;
/**
* #var \DateTime $updated_at
*
* #ORM\Column(type="datetime")
*/
private $updated_at;
}
Then, I extend my class:
<?php
namespace AppBundle\Entity;
use AppBundle\Model;
/**
* Entity
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="RB\ProductsBundle\Entity\ProductRepository")
*/
class Entity extends Model
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
}
The problem is: When I make a php app/console doctrine:schema:update --dump-sql, it doesn't recognize the Model fields. How can I change that? Should I use a trait or something like that?
EDIT: I tried to add * #ORM\MappedSuperclass at the top of my abstract class with no luck
The problem is likely to be that your properties in the abstract parent class are defined as private. Change them to protected and it should work.
As an aside, I use traits for this purpose, rather than inheritance, as your entity classes can use multiple traits, but they can only extend one abstract class.

How to remove usage of cascade={"remove","persist"} from entities in Symfony2?

I am working on a project where there I am forced to use cascade={"remove","persist"} because of the problem described here.
Reading through the documentation, to quote:
Even though automatic cascading is convenient it should be used with care. Do not blindly apply cascade=all to all associations as it will unnecessarily degrade the performance of your application. For each cascade operation that gets activated Doctrine also applies that operation to the association, be it single or collection valued.
And I see that the same can be fixed if I use
$em->persist($entity);
In my persistence services, which I am already calling. However, doctrine doesn't work as expected. Here are my entities.
Entity/Employee.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
* #ORM\Table(uniqueConstraints={#ORM\UniqueConstraint(
* name="UNIQ_EMPLOYEE_ID_NAME_ADDRESS_STATE_CITY_COUNTRY",
* columns={"id","name","city","state","country"}
* )})
*/
class Employee
{
/**
* #ORM\Id
* #ORM\Column(type="integer", options={"unsigned":true})
* #ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\Column(type="text", nullable=false)
*/
private $address;
/**
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $city;
/**
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $state;
/**
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $country;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Department", inversedBy="employee", cascade={"remove","persist"})
* #ORM\JoinColumn(name="department_id", referencedColumnName="id", nullable=false)
*/
private $department;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Transfer", mappedBy="employee", cascade={"remove","persist"})
*/
private $transfer;
}
?>
Entity/Department.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Selectable;
/**
* #ORM\Entity
* #ORM\Table(uniqueConstraints={#ORM\UniqueConstraint(
* name="UNIQ_DEPARTMENT_ID_NAME",
* columns={"id","name"}
* )})
*/
class Department
{
/**
* #ORM\Id
* #ORM\Column(type="integer", options={"unsigned":true})
* #ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Employee", mappedBy="department", cascade={"remove","persist"})
*/
private $employee;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Transfer", mappedBy="department", cascade={"remove","persist"})
*/
private $transfer;
}
?>
Entity/Transfer.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Selectable;
/**
* #ORM\Entity
* #ORM\Table(uniqueConstraints={#ORM\UniqueConstraint(
* name="UNIQ_TRANSFER_ID_DEPARTMENT_EMPLOYEE_START_END",
* columns={"id","name"}
* )})
*/
class Transfer
{
/**
* #ORM\Id
* #ORM\Column(type="integer", options={"unsigned":true})
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="date", length=255, nullable=false)
*/
private $start;
/**
* #ORM\Column(type="date", length=255, nullable=false)
*/
private $end;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Employee", inversedBy="attendance", cascade={"persist","remove"})
* #ORM\JoinColumn(name="employee_id", referencedColumnName="id", nullable=false)
*/
private $employee;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Department", inversedBy="attendance", cascade={"persist","remove"})
* #ORM\JoinColumn(name="department_id", referencedColumnName="id", nullable=false)
*/
private $department;
}
?>
UPDATE 1:
Now, I have another problem. Since my GeneratorValue strategy for Employee and Department are NONE, I have problem with duplicate record error. I am trying to use PreFlushEventArgs to remove the entity before persisting if the record exists in database. But I wonder if it should be that complex?
Thanks for your help in advance.
It seems you cascade persist operation from Employee to Department and Transfer. But they do cascade the persist operation to the Employee Entity too.
It means when you do
$em->persist($an_employee);
You are stuck in a persist loop.
In my opinion, the cascade persist should be only in one way, i.e. only on the Employee entity.
Also if you choose to do this, add your Department to the Employee, and not the opposite :
$an_employee->addDepartment($a_department);
$an_employee->addTransfer($a_transfer);
That way, when you persist an employee, its Departments and Transfer should be persisted too

Namespace "..." does not contain any mapped entities

when i'm trying to create entitiy from console i give this error:
Namespace "..." does not contain any mapped entities
So,
Here is my code
Which line is wrong, please tell me. Thanks.
namespace SfTuts\JobeetBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="job")
*/
class Job
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="Category")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
/**
* #ORM\Column(type="string", length=255)
*/
protected $type;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
protected $company;
/**
* #ORM\Column(type="string", length=255)
*/
protected $logo;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
protected $url;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
protected $position;
/**
* #ORM\Column(type="string", length=255)
*/
protected $location;
/**
* #ORM\Column(type="string", length=4000)
*/
protected $description;
/**
* #ORM\Column(type="string", length=4000, name="how_to_apply")
*/
protected $howToApply;
/**
* #ORM\Column(type="string", length=255, unique=true)
*/
protected $token;
/**
* #ORM\Column(type="boolean", name="is_public")
*/
protected $isPublic;
/**
* #ORM\Column(type="boolean", name="is_activated")
*/
protected $isActivated;
/**
* #ORM\Column(type="string", length=255)
*/
protected $email;
/**
* #ORM\Column(type="datetime", name="created_at")
*/
protected $createdAt;
/**
* #ORM\Column(type="datetime", name="updated_at")
*/
protected $updatedAt;
/**
* #ORM\Column(type="datetime", name="expires_at")
*/
protected $expiresAt;
public function __construct()
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
}
How can i solve this. Thanks.
Use
doctrine:generate:entity
To create a new entity.
Then after adding your own properties by editing the file, use
doctrine:generate:entities AcmeDemoBundle:MyEntity
To create getters/setters
I found that if you change directories to the project direcotry, then add the path option to the command,it works everytime. I ran:
sudo app/console doctrine:generate:entities --path=./ Blog
while working on the symblog.dev tutorial. That's what it took to work
I found namespace needs to match up to your entity. Double check your slashes.
app/console doctrine:generate:entities SfTuts/JobeetBundle/Entity
You probably used a path instead of the namespace.
Execute the following rule:
app/console doctrine:generate:entities SfTuts/JobeetBundle/Entity --no-backup
I found that this error will arise if you forgot to add your bundle to the AppKernel class.
After adding mine, simply using console doctrine:generate:entities MyBundle worked fine.

not able to view foreign key constrain in database

i am using symfony 2 and doctrine to create enitity class with a foreign key
here is my entity class , now the issue is when i check the table fos_user in mysql database , i don't see travel_access_token and travel_id as a foreign key and even if i change the value of travel_id in fos_user table database is not raising any issues.
i have one to one unidirectional relation as only one user can have one token only
<?php
namespace Travel\HomeBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/** #ORM\Column(name="facebook_id", type="string", length=255, nullable=true) */
protected $facebook_id;
/** #ORM\Column(name="facebook_access_token", type="string", length=255, nullable=true) */
protected $facebook_access_token;
/**
#ORM\Column(name="travel_id", type="string", length=255, nullable=true)
#ORM\OneToOne(targetEntity="Travel\HomeBundle\Entity\Client")
#ORM\JoinColumn(name="travel_id", referencedColumnName="id")
*/
protected $travel_id;
/**
#ORM\Column(name="travel_access_token", type="string", length=255, nullable=true)
#ORM\OneToOne(targetEntity="Travel\HomeBundle\Entity\Client")
#ORM\JoinColumn(name="travel_access_token", referencedColumnName="secret")
*/
protected $travel_access_token;
public function __construct()
{
parent::__construct();
// your own logic
}
}
?>
As "targetEntity" try use just the names of entities. Like this:
#ORM\OneToOne(targetEntity="Client")

Resources