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

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.

Related

Generating table schema from entities

I am using Skipper ORM to design/generate entities - everything exports fine - then I do:
app/console doctrine:database:drop --force
app/console doctrine:database:create
app/console doctrine:schema:update --force
app/console doctrine:generate:entities --no-backup MyNamespace/MyBundle
I am receiving an error/exception:
[Doctrine\DBAL\Schema\SchemaException]
There is no column with name 'alternateId' on table 'inventory'.
What gives? The entity in question certainly does have the field:
<?php
namespace Company\DistributionBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity(repositoryClass="Company\DistributionBundle\Repository\InventoryRepository")
* #ORM\Table(
* name="inventory",
* indexes={#ORM\Index(name="ALTID", columns={"alternateId"})},
* uniqueConstraints={
* #ORM\UniqueConstraint(name="PKID", columns={"id"}),
* #ORM\UniqueConstraint(name="FINDID", columns={"partNumber","partDescription"})
* }
* )
*/
class Inventory
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="integer", nullable=false)
*/
private $alternateId;
/**
* #ORM\Column(type="integer", nullable=false)
*/
private $unitOftMeasure;
/**
* #ORM\Column(type="integer", nullable=false)
*/
private $minimumQuantity;
/**
* #ORM\Column(type="integer", nullable=false)
*/
private $maximumQuantity;
/**
* #ORM\Column(type="decimal", nullable=false)
*/
private $maximumCycles;
/**
* #ORM\Column(type="decimal", nullable=false, precision=10, scale=4)
*/
private $maximumTime;
/**
* #ORM\Column(type="decimal", nullable=false, precision=10, scale=2)
*/
private $listPrice;
/**
* #ORM\Column(type="date", nullable=false)
*/
private $datePrice;
/**
* #ORM\Column(type="string", length=50, nullable=false)
*/
private $partNumber;
/**
* #ORM\Column(type="string", length=50, nullable=false)
*/
private $partDescription;
/**
* #ORM\Column(type="text", length=255, nullable=true)
*/
private $partNotes;
/**
* #ORM\OneToOne(targetEntity="Company\DistributionBundle\Entity\Application", inversedBy="inventory")
* #ORM\JoinColumn(name="application_id", referencedColumnName="id", nullable=false, unique=true)
*/
private $application;
/**
* #ORM\OneToOne(targetEntity="Company\DistributionBundle\Entity\Category", inversedBy="inventory")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false, unique=true)
*/
private $category;
/**
* #ORM\OneToOne(targetEntity="Company\DistributionBundle\Entity\Manufacturer", inversedBy="inventory")
* #ORM\JoinColumn(name="manufacturer_id", referencedColumnName="id", nullable=false, unique=true)
*/
private $manufacturer;
/**
* #ORM\ManyToMany(targetEntity="Company\DistributionBundle\Entity\InventoryOptions", mappedBy="inventory")
*/
private $inventoryOptions;
}
Try using the doctrine migrations bundle for two reasons:
It helps you manage your production database and all the versions of it. You can check any version any time
It Beautifully creates 'sql migrations' for you. Which you can use to (run migrations) to make changes in database.
Also, it understand your entities, read the annotations and creates the required files.
Go ahead and explore it.

Entity inheritance in Doctrine doesn't include OneToOne-relationships

Trying to utilize inheritance, I've created the following entities:
/**
* #ORM\Table(name="persons")
* #ORM\Entity()
*/
class Person
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* #ORM\OneToOne(targetEntity="Image", cascade={"persist"})
* #ORM\JoinColumn(name="image_id", referencedColumnName="id")
*/
protected $image;
}
/**
* #ORM\Table(name="actors")
* #ORM\Entity()
*/
class Actor extends Person
{
/**
* #ORM\Column(name="character", type="string", length=255)
*/
private $character;
}
/**
* #ORM\Table(name="images")
* #ORM\Entity()
*/
class Image
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="path", type="string", length=255)
*/
private $path;
}
Which almost works perfectly. The generated actors-table contains all the persons-fields, except for the image-relation. I've tried to change the relation to a ManyToOne, which didn't help.
How to make the Actor-entity also inherit all joined fields? I'm open to other solutions, if the above isn't ideal.
You need a parent construct in your Actor class:
public function __construct()
{
parent::__construct();
// your own logic
}
It is advised that you add an ID aswell.

does not contain any mapped entities while generating getter and setter

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

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")

Symfony2 + OneToMany - read from database error

I'm trying to learn read/write data to/from database, and I have a huge problem :
My entities looks :
Kategorie:
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="kategorie")
*/
class Kategorie
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $idkategorii;
/**
* #ORM\Column(type="string", length=50)
*/
protected $nazwa;
/**
* #ORM\OneToMany(targetEntity="Ogloszenia", mappedBy="ogloszenia")
*/
protected $ogloszenia;
Ogloszenia:
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="ogloszenia")
*/
class Ogloszenia
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string",length=100)
*/
protected $tytul;
/**
* #ORM\Column(type="string", length=120)
*/
protected $tytul_seo; //tytul bez polskich znaków etc.
/**
* #ORM\Column(type="text")
*/
protected $tresc; //tresc ogloszenia
/**
* #ORM\Column(type="string",length=50)
*/
protected $dodal; //imie osoby ktora dodala ogloszenie
/**
* #ORM\Column(type="string", length=50)
*/
protected $kontakt; //nr tel lub mail
/**
* #ORM\ManyToOne(targetEntity="Kategorie", inversedBy="kategoria")
* #ORM\JoinColumn(name="kategoria", referencedColumnName="idkategorii")
*/
protected $kategoria;
Now, in my controller i'm trying to read all values :
public function odczytajAction()
{
$id = 1;
$kategoria = $this->getDoctrine()
->getRepository('FrontendOgloszeniaBundle:Kategorie')
->find($id);
$ogl = $kategoria->getOgloszenia();
foreach($ogl as $o)
{
print_r($o);
}
return new Response('test odczytu');
}
And unfortunately symfony2 gives me an following error
Notice: Undefined index: ogloszenia in /home/sl4sh/public_html/Projekt1/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1574
500 Internal Server Error - ErrorException
So, please tell me what's wrong with mine code ?
You have invalid mappedBy annotation:
/**
* #ORM\OneToMany(targetEntity="Ogloszenia", mappedBy="kategoria")
*/
protected $ogloszenia;
And also inverse side:
/**
* #ORM\ManyToOne(targetEntity="Kategorie", inversedBy="ogloszenia")
* #ORM\JoinColumn(name="kategoria", referencedColumnName="idkategorii")
*/
protected $kategoria;

Resources