Skip to content
William Espindola edited this page Sep 23, 2015 · 27 revisions

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

Work with doctrine

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

Instance

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

Saving

To create or update a field

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

To create or update a collection

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

To create or update a language

$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

Finding

Find one

$repository->find(1); // by id

Find by

$repository->findBy(['name' => 'field-name'], ['name' => 'ASC']);

Find all

$repository->findAll();

Work with relational

The connection and setup work like traditional relational works

Instance

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

Saving

To create or update a Field

$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

To create or update a Collection

$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

To create or update a language

$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

Finding

The criteria use Respect\Relational Convention in this case

Find one

$repository->find(1); // by id

Find by

$repository->findBy(['name' => 'field-name'], ['order by name ASC']);

Find all

$repository->findAll();