Accessible can also manage the relations between your classes. This feature resides in the two following annotation:
[@Referenced](https://github.com/Referenced): Indicates that the associated object (or objects if the property is a collection) has a reference to the current object.[@InCollection](https://github.com/InCollection): Indicates that the associated object (or objects if the property is a collection) has a collection in which the current object resides.Here is a simple example of how it can be used:
use Accessible\Annotation\Inverted;
class Student
{
/**
* [@Access](https://github.com/Access)({Access:GET, Access::SET})
* [@Referenced](https://github.com/Referenced)(className=Bag::class, propertyName="owner")
*/
private $bag;
}
class Bag
{
/**
* [@Access](https://github.com/Access)({Access:GET, Access::SET})
*/
private $owner;
}
$student = new Student();
$bag = new Bag();
$student->setBag($bag);
$bag->getStudent(); // -> $student
In this example, when a new value to Student#bag, the property Bag#owner of the previous bag will be set to null, and to $student in the new bag.
If you add another [@Referenced](https://github.com/Referenced) annotation in Bag#owner, modifying this property will also modify Student#bag.
Note that if you have a Many to One or a Many to Many relationship, the properties defined as collections must have a [@ListBehavior](https://github.com/ListBehavior) or a [@SetBehavior](https://github.com/SetBehavior) annotation.
This section gives examples of how you can manage the relations between your classes, following the relationship vocabulary you can find with Doctrine.
In this example, a student has a bag, and a bag belongs to a student.
use Accessible\Annotation\Inverted;
class Student
{
/**
* [@Access](https://github.com/Access)({Access:GET, Access::SET})
* [@Referenced](https://github.com/Referenced)(className=Bag::class, propertyName="owner")
*/
private $bag;
}
class Bag
{
/**
* [@Access](https://github.com/Access)({Access:GET, Access::SET})
* [@Referenced](https://github.com/Referenced)(className=Student::class, propertyName="bag")
*/
private $owner;
}
$student = new Student();
$bag = new Bag();
$student->setBag($bag);
$bag->getOwner(); // -> $student
$bag->setOwner(new Student());
$student->getBag(); // -> null
In this example, a student has several books, and a book belongs to a student.
use Accessible\Annotation\Inverted;
use Accessible\Annotation\Mapped;
class Student
{
/**
* [@Access](https://github.com/Access)({Access:GET, Access::SET})
* [@ListBehavior](https://github.com/ListBehavior)
* [@Referenced](https://github.com/Referenced)(className=Book::class, propertyName="owner")
* [@Initialize](https://github.com/Initialize)({})
*/
private $books;
}
class Book
{
/**
* [@Access](https://github.com/Access)({Access:GET, Access::SET})
* [@InCollection](https://github.com/InCollection)(className=Student::class, propertyName="books")
*/
private $owner;
}
$student = new Student();
$book = new Book();
$student->addBook($book);
$book->getOwner(); // -> $student
$book->setOwner(new Student());
$student->getBag(); // -> []
In this example, a student has several teachers, and a teacher teaches to several students.
use Accessible\Annotation\Mapped;
class Student
{
/**
* [@Access](https://github.com/Access)({Access:GET, Access::SET})
* [@SetBehavior](https://github.com/SetBehavior)
* [@InCollection](https://github.com/InCollection)(className=Teacher::class, propertyName="students")
* [@Initialize](https://github.com/Initialize)({})
*/
private $teachers;
}
class Teacher
{
/**
* [@Access](https://github.com/Access)({Access:GET, Access::SET})
* [@SetBehavior](https://github.com/SetBehavior)
* [@InCollection](https://github.com/InCollection)(className=Student::class, propertyName="teachers")
* [@Initialize](https://github.com/Initialize)({})
*/
private $students;
}
$student = new Student();
$teacher = new Teacher();
$student->addTeacher($teacher);
$teacher->getStudents(); // -> [$student]
$teacher->removeStudent($student);
$student->getTeachers(); // -> []
If you want to manage your class associations manually, you can use the updatePropertyAssociation() method. Here is an example of how to use it:
class Student
{
/**
* [@Access](https://github.com/Access)({Access:GET, Access::SET})
* [@Referenced](https://github.com/Referenced)(className=Bag::class, propertyName="owner")
*/
private $bag;
public function setBag(Bag $bag)
{
$valuesToUpdate = [
'oldValue' => $this->bag,
'newValue' => $bag
];
$this->bag = $bag;
$this->updatePropertyAssociation('bag', $valuesToUpdate);
}
}
Note that $valuesToUpdate could also only be composed of one of these two values (and could even be empty).
How can I help you explore Laravel packages today?