-
Notifications
You must be signed in to change notification settings - Fork 2
Repository
Today you can use respect\relational or doctrine/orm as ORM. Just add on your composer.json the ORM that work for you. See the composer.json file to check the compatible versions.
Available repositories
use WilliamEspindola\Field\Repository\FieldRepository;
use WilliamEspindola\Field\Repository\CollectionRepository;
use WilliamEspindola\Field\Repository\LanguageRepository;
use WilliamEspindola\Field\Repository\OptionRepository;
Work with doctrine
Work with relational
To work with Doctrine exists a couple of xml config files for the entities Collection, Field, CollectionField and Option. You can see here. The connection and setup work like traditional doctrine works
use Doctrine\ORM\Tools\Setup;
use WilliamEspindola\Field\Storage\ORM\Doctrine;
use WilliamEspindola\Field\Repository\FieldRepository;
$conn = ['driver' => '', 'user' => '', 'dbname' => '', 'password' => ''];
$configXml = __DIR__ . "vendor/williamespindola/field/config/xml";
$setUp = Setup::createXMLMetadataConfiguration([$configXml], true);
$storage = new Doctrine($yourConnection, $setUp);
$fieldRepository = new FieldRepository($storage);
use WilliamEspindola\Field\Entity\Field;
$field = new Field();
$field->setName('field-name');
$field->setType('text');
$field->setLabel('Field Name');
$field->setLanguage($languageEntity);
$fieldRepository->save($field);
If the entity has id the save method will update
use WilliamEspindola\Field\Entity\Collection;
$collectionRepository = new CollectionRepository($storage);
$collection = new Collection();
$collection->setName('collection-name');
$collection->setLabel('Collection Name');
$collection->setLanguage($languageEntity);
$collectionRepository->save($collection);
If the entity has id the save method will update
$languageRepository = new LanguageRepository($storage);
$language = new Language();
$language->setName('pt_BR');
$language->setLabel("Português Brasileiro");
$languageRepository->save($language);
If the entity has id the save method will update
$repository->find(1); // by id
$repository->findBy(['name' => 'field-name'], ['name' => 'ASC']);
$repository->findAll();
The connection and setup work like traditional relational works
use Respect\Relational\Mapper;
use WilliamEspindola\Field\Storage\ORM\RespectRelational;
$mapper = new Mapper(new PDO(/* string connection */));
$storage = new RespectRelational($mapper);
$fieldRepository = new FieldRepository($storage);
$field = (object)([
'id' => null, // set the id to update
'name' => 'field-name',
'type' => 'text',
'label' => 'Field Name',
'language_id' => '1' // id of language
]);
$fieldRepository-save($field);
If the entity has id the save method will update
$collectionRepository = new CollectionRepository($storage);
$collection = (object)([
'name' => 'collection-name',
'label' => 'Collection name',
'language_id' => '1' // id of language
]);
$collectionRepository->save($collection);
If the entity has id the save method will update
$languageRepository = new LanguageRepository($storage);
$language = (object)([
'name' => 'pt_BR',
'label' => 'Português Brasileiro'
]);
$languageRepository->save($language);
If the entity has id the save method will update
The criteria use Respect\Relational Convention in this case
$repository->find(1); // by id
$repository->findBy(['name' => 'field-name'], ['order by name ASC']);
$repository->findAll();