I have 2 bundles, 1 CMS bundle that will be the parent bundle.
I have in both bundles duplicated entitys. Like User The user in the CMS bundle i made it a abstract class. (not sure if that is the right choice. Actually, what I want is extending my user entity IF needed.).
cms user:
abstract class User implements UserInterface
bundle user:
use MV\CMSBundle\Entity\User as BaseUser;
/**
* #ORM\Entity(repositoryClass="MV\NameBundle\Repository\UserRepository")
* #DoctrineAssert\UniqueEntity(fields={"email"}, message="user.email.already.exist" )
*/
class User extends BaseUser
{
....
}
Im getting the error Class "MV\CMSBundle\Entity\User" is not a valid entity or mapped super class.
I have searched in the documentation of symfony and found this page: entities-entity-mapping but they didn't add some content xD
Oh, and no I dont want to use FOSUserBundle ;)
Symfony: 2.1
In my case I was missing * #ORM\Entity in my class definition.
/**
* #ORM\Entity
* #ORM\Table(name="listtype")
*/
class ListType
{
...
}
Define the base-class as follows:
/**
* #ORM\MappedSuperclass
*/
abstract class BaseUser
{
// ...
}
Define the real entity:
/**
* #ORM\Entity
*/
class User extends BaseUser
{
// ...
}
Because you're missing the #MappedSuperclass annotation on the base-class, Doctrine throws the exception you mention.
In my case, the problem was eaccelerator because it strips out all the comments which Doctrine uses. After disabling eaccelerator it worked . You can disable your php settings or,
in the web/app_dev.php or web/app.php file.
<?php
ini_set('eaccelerator.enable', 0);
ini_set('eaccelerator.optimizer', 0);
//rest of the code.
Note: Do clear the symfony2 cache after disabling this.
I had the same problem. But to make it work but, I had to shift the lines:
* #ORM\Table
* #ORM\Entity
Related
I'm trying to extend this IndexRepository to add my own method for a special search.
In the controller I inject my own IndexRepository with:
use Webian\Iancalendar\Domain\Repository\IndexRepository;
/**
* Inject index repository.
*
* #param IndexRepository $indexRepository
*/
public function injectIndexRepository(IndexRepository $indexRepository)
{
$this->indexRepository = $indexRepository;
}
What I did is working but I get this warning:
PHP Warning
Core: Error handler (BE): PHP Warning: Declaration of Webian\Iancalendar\Controller\
BackendController::injectIndexRepository(Webian\Iancalendar\Domain\Repository\IndexRepository $indexRepository)
should be compatible with HDNET\Calendarize\Controller\
AbstractController::injectIndexRepository(HDNET\Calendarize\Domain\Repository\IndexRepository $indexRepository)
in /typo3conf/ext/iancalendar/Classes/Controller/BackendController.php line 42
That's because I'm using my own Webian\Iancalendar\Domain\Repository\IndexRepository that extends HDNET\Calendarize\Domain\Repository\IndexRepository. If I use the original one the warning doesn't appear but obviously my own method is not called.
How can I avoid that warning?
You should either not extend HDNET\Calendarize\Controller\AbstractController but the default AbstractController of Extbase, then you will need to implement all required logic yourself.
Or you just use a different name for your injection method:
use HDNET\Calendarize\Controller\AbstractController;
use MyNamespace\MyExtension\Domain\Repository\IndexRepository;
class MyController extends AbstractController
{
...
/**
* The index repository.
*
* #var IndexRepository
*/
protected $myIndexRepository;
/**
* Inject index repository.
*
* #param IndexRepository $myIndexRepository
*/
public function injectMyIndexRepository(IndexRepository $myIndexRepository)
{
$this->myIndexRepository = $myIndexRepository;
}
...
class IndexRepository extends \HDNET\Calendarize\Domain\Repository\IndexRepository
{
...
// My method that extends \HDNET\Calendarize\Domain\Repository\IndexRepository functionalities
public function findByStartDate(DateTime $startDate = null, DateTime $endDate = null)
{
...
The method name does not really matter, only that it starts with inject and has a type hint indicating the dependency to inject.
I'm using MongoDB with Doctrine for a Symfony project.
For a document (I call it class Product) I need to define a field (e.g. name), but I want to act doctrine in a way that the document cannot be saved without defining a it. Meaning, whenever creating or updating a new Product, the property $name has to be defined.
For doctrine and MySql this is quite simple. There you have to say:
class Product {
...
/** #ORM\Column(type="string", nullable=false) */
private $name;
...
}
Is there a similar way in MongoDb?
The following code (which I found in a similar thread) doesn't work:
class Product {
...
/**
* #MongoDB\Field(type="string")
* #Constraints\NotBlank
*/
protected $name;
...
}
I'm using the Zend Standard Autoloader. It's registering one namespace, but it won't register the other. This is my code:
$zflib = $_SERVER['SERVER_ROOT'].'/classes/Zend_Framework_2/2.3.2/library';
require_once($zflib.'/Zend/Loader/StandardAutoloader.php');
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
// Library
$loader->registerNamespace('VProd',$_SERVER['SERVER_ROOT'].'/classes/VProd');
// Dealer Library
$loader->registerNamespace('Dealers', $_SERVER['SERVER_ROOT'].'/dealers/classes');
$loader->setFallbackAutoloader(true);
$loader->register();
I then have basic setup like this in the dealers/classes directory:
Model.php
<?php
namespace Dealers\Models;
class Model {
/**
* The table this model uses
*
* #var string
*/
protected $table;
}
Coop Model:
<?php
namespace Dealers\Models\Coop;
use Dealers\Models\Model;
class Coop extends Model {
/**
* The table this model uses
*
* #var string
*/
protected $table = 'coop';
public static function testing()
{
return 'testing';
}
}
In my application I'm including that config file BEFORE anything else.
coop.php
<?php
require_once($_SERVER['SERVER_ROOT'].'/security/config.php');
use Dealers\Model\Coop;
echo CoopBalance::testing();
I'm getting this error message:
Fatal error: Class 'Dealers\Models\Model' not found in
\www\dealers\classes\coop\Coop.php on line 7
Which is where this line is in my Coop Model:
class Coop extends Model
Thanks for any help!
The ZF2 standard autoloader is a PSR-0 compliant autoloader. So your classes should be at dealers/classes/Dealers/Models/Model.php and dealers/classes/Dealers/Models/Coop/Coop.php (case sensitive) to get autoloaded properly (each 'part' of the namespace should be a folder).
Also, if at all possible I'd recommend using Composer to install ZF2 (or whichever components you're using). Then you wouldn't need to configure the autoloader yourself at all.
I used Symfony2 and Doctrine MongoDBBundle and I have simple Single Collection Inheritance classes. How can I know what type of document it is in a twig template? For example the base class is Entity and extended by User and Organization, in listing those in a twig template I'd like to know what type of entity it is (i.e. whether it's a User or an Organization). I wonder if it's possible to get the value of the DiscriminatorField of the document.
/**
* #MongoDB\Document(collection="entity")
* #MongoDB\InheritanceType("SINGLE_COLLECTION")
* #MongoDB\DiscriminatorField(fieldName="type")
* #MongoDB\DiscriminatorMap({"user"="User", "shop"="Shop"})
*/
class Entity
{
/**
* #MongoDB\Id
*/
protected $id;
protected $entityType;
public function getEntityType()
{
return $this->entityType;
}
}
Can anybody tell me whether its posible to override doctrine2 persistentobject magic getters\setters? i'd like to do the below:-
public function setDob($dob)
{
$this->dob= new \Date($date);
}
however my entity is defined as:-
use Doctrine\Common\Persistence\PersistentObject;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity(repositoryClass="Ajfit\Repository\User")
* #ORM\HasLifecycleCallbacks
*/
class User extends \Doctrine\Common\Persistence\PersistentObject
{
/**
* #var date $dob
*
* #ORM\Column(name="dob", type="date")
*/
protected $dob;
}
the public function setDob does not get called when I create the entity using:-
public function getNewRecord() {
return $this->metadata->newInstance();
}
I get the below error:-
Notice:- array to string conversion ...Doctrine\DBAL\Statement.php on line 98
Any help would be much apprieciated.
Thanks
Andrew
__call of PersistentObject#__call will not be called if you defined the setDob method.
What you're doing there is creating a new instance via metadata. What you are doing there is probably assuming that __construct or any setter/getter should be called by the ORM. Doctrine avoids to call any methods on your object when generating it via metadata/hydration (check ClassMetadataInfo#newInstance to see how it is done) as it does only know it's fields.
This allows you to be completely independent from Doctrine's logic.
About the notice, that is a completely different issue coming from Doctrine\DBAL\Statement, which suggests me that you have probably some wrong parameter binding in a query. That should be handled separately.