Doctrine 2 integration plugin.
Contents
- PHP 7.2+
- SunLight CMS 8
Entities need to be registered using the doctrine.map_entities event before they can be used.
Table are automatically prefixed with db.prefix as defined in the global config.php.
If no table name is explicitly specified, UnderscoreNamingStrategy is used to generate one.
An optional base entity class is available. It provides several active record-esque utility methods.
find($id)- try to find an instance by IDgetRepository()- get repository for the entity classgetEntityManager()- get the entity manager (protected)
persist()- make the entity managed and persistentsave()- persist and flush the entity to the databasedelete()- delete and flush this entity from the database
<?php
namespace SunlightExtend\Example\Entity;
use Doctrine\ORM\Mapping as ORM;
use SunlightExtend\Doctrine\Entity;
/**
* @ORM\Table(name="example_foo")
*/
class Foo extends Entity
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var int
*/
public $id;
/**
* @ORM\Column(type="string", length=64)
*
* @var string
*/
public $name;
function __construct(string $name)
{
$this->name = $name;
}
}<?php
// create a new entity
$foo = new Foo('test');
$foo->save();
var_dump($foo->id);
// find existing entities
Foo::find(1); // by ID
Foo::getRepository()->findBy(['name' => 'test']); // using repository
// saving changes
$foo->name = 'new name';
$foo->save();
// deleting entities
$foo->delete();<?php
use SunlightExtend\Doctrine\DoctrineBridge;
$em = DoctrineBridge::getEntityManager();Called when Doctrine is being initialized.
Arguments:
connection- instance ofDoctrine\DBAL\Connectionconfig- instance ofDoctrine\ORM\Configurationmetadata_driver- instance ofDoctrine\Common\Persistence\Mapping\Driver\MappingDriverChainevent_manager- instance ofDoctrine\Common\EventManager
Called when Doctrine entities should be registered.
Arguments:
mapping- reference to the entity mapping array
Example:
<?php
// register all entities in the Entity subnamespace using annotations
$args['mapping']['annotation'][__NAMESPACE__ . '\\Entity\\'] = __DIR__ . '/Entity';
// register all entities in the Entity subnamespace using XML files
$args['mapping']['annotation'][__NAMESPACE__ . '\\Entity\\'] = __DIR__ . '/Resources/doctrine';See:
Doctrine console can be accessed by clicking on the plugin's "Console" action in "Administration - Plugins".