Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine + CodeIgniter: after generate entities with namespace, "cannot redeclare class" on schema create
    primarykey
    data
    text
    <p>I'm trying to generate both entities and the database schema with Doctrine under CodeIgniter 2.0, using annotations. I'm not sure if I'm doing this correctly. I've setup base classes which have only the classname and properties defined, and then annotated. I'm trying to use the generate-entities command to have it create the get/set methods.</p> <pre><code>&lt;?php namespace Entities; include('ContentItem.php'); /** * @Entity * @Table(name="blobs") */ class Blob extends ContentItem { /** * @var ContentItemId * * @ManyToOne(targetEntity="ContentItem") * @JoinColumn(name="contentitem_id", referencedColumnName="id") */ protected $contentitem; /** * @Column(type="string", length=64, nullable=true) */ protected $content; /** * @Column(name="date_created", type="datetime", nullable=false) */ protected $date_created; /** * @Column(type="datetime", nullable=false) */ protected $date_updated; } </code></pre> <p>This file is located in a folder named 'models'. If I run the command 'orm:generate-entities models', then it creates new classes inside of '/models/Entities' folder. This only happens because of the 'namespace Entities' declaration. If I run 'orm:schema-tool:create' to try and create my database schema, I get: "Fatal error: Cannot redeclare class Entities\Blob in /Users/rw3iss/Sites/ci/application/models/Entities/Blob.php on line 11". This is happening because the generated 'models/Entities' classes inherit the namespace from the files they're generated from in the parent 'models' folder, hence the dual class conflict.</p> <p>Now if I remove the namespace declaration from the parent files, and call generate entities, passing 'models/Entities' as the destination, I get the same file layout. Except then, when trying to use the models in my code:</p> <pre><code>$blob = new Entities\Blob; </code></pre> <p>this throws the same error, obviously because there are still two classes with the same name, without a namespace.</p> <p>I have declared the Entities class loader like this:</p> <pre><code>$entityClassLoader = new \Doctrine\Common\ClassLoader('Entities', APPPATH.'models'); </code></pre> <p>So my question is:</p> <p>Is there anyway to generate these entity classes from a basic class (which has my annotations declared for the class and properties), preserving namespaces, so that I can still instantiate them using 'new Entities\Blob'? If there was even a way to tell the generated classes to use a different namespace from the file's they're generated from, that would be fine. I'd like to setup basic properties, and have the 'generate entities' command create my get/set methods for me. It seems that whenever I use namespaces, it will auto-create the classes inside of the namespace subfolders, and then I get the 'cannot redeclare class' error. </p> <p>Maybe I'm doing something fundamentally wrong...</p> <p>Thanks for any help!</p> <p><strong>UPDATE:</strong> Solution:</p> <p>I may have gotten it working by putting the Entities folder side by side with my models folder. If I change the Entities ClassLoader to point to just 'APPPATH', and then instantiate my Entities using 'new Entities\Blob', then that works. It seems like there is no way to put the Entities folder as a subfolder within the models folder without it complaining in some way. If anyone know of a way to do this, I'd be interested to hear.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload