1. Go to this page and download the library: Download webiny/entity library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// Long syntax
$page = Page::findById("53712ed46803fa4e058b456b");
$title = $page->getAttribute('title')->getValue();
// Short syntax
$page = Page::findById("53712ed46803fa4e058b456b");
$title = $page->title;
$page = Page::findById("53712ed46803fa4e058b456b");
$authorName = $page->author->firstName . ' '. $page->author->lastName;
// $authorName will be equal to 'John Doe'
// provides you with autocomplete on AbstractAttribute methods
$page->getAttribute('title')->setValue('New title');
// or (no autocomplete is the only downside of this)
$page->title->setValue('New title');
// or - This will trigger `__set` magic method and call $page->getAttribute('title')->setValue('New title');
$page->title = 'New title';
$page = Page::findById("53712ed46803fa4e058b456b");
echo $page->comments->count();
foreach($page->comments as $comment){
...
}
// Load Page
$page = Page::findById("53712ed46803fa4e058b456b");
// Create new Comment
$comment = new Comment();
$comment->populate($commentData);
// Assign and save Page
$page->comments->add($comment);
$page->save();
// Load Page
$page = Page::findById("53712ed46803fa4e058b456b");
// Create new Comment
$comment = new Comment();
$comment->populate($commentData);
// Set Page instance and save
$comment->page->setValue($page);
$comment->save();
// $page->settings is an instance of ArrayAttribute
// Set value by specifying whatever nesting level you want
// Even if some of the levels may not exist along the way - they will be created for you automatically
$page->settings->set('level1.level2.level3', 'My new value');
// Get value from key that may not exist
$page->settings->get('level1.level4', 'Default value');
// You can also append values like this
$page->settings[] = 'New value';
// Or using an 'append' method
$page->settings->append('New value');
// And you can also prepend values
$page->settings->prepend('New value');
// Provide a default entity ID (can be fetched from logged in user, or retrieved from database or whatever)
$defaultId = '53712ed46803fa4e058b456b';
$this->attr('author')->many2one()->setEntity('Author')->setDefaultValue($defaultId);
// Provide a default entity instance
$defaultInstance = Author::find(['some' => 'condition']);
// or
$defaultInstance = Author::findById('53712ed46803fa4e058b456b');
$this->attr('author')->many2one()->setEntity('Author')->setDefaultValue($defaultInstance);
// Provide data which will be used to populate new entity instance
// (will create a new record with new ID each time a default value is used)
$defaultData = [
'firstName' => 'Pavel',
'lastName' => 'Denisjuk'
];
$this->attr('author')->many2one()->setEntity('Author')->setDefaultValue($defaultData);
Array
(
[author] => Array
(
[name] => John Doe
)
[title] => First blog post
[comments] => Array
(
[0] => Array
(
[text] => Best blog post ever!
[id] => 53dee8d26803fafe098b4569
)
)
[labels] => Array
(
[0] => Array
(
[label] => marketing
[id] => 53dee8d26803fafe098b456b
)
[1] => Array
(
[label] => seo
[id] => 53dee8d26803fafe098b456e
)
)
)
$data = $page->toArray('*,comments');
Array
(
[author] => Arrayf
(
[id] => 53dee8d26803fafe098c4769
[name] => John Doe
)
[title] => First blog post
[comments] => Array
(
[0] => Array
(
[text] => Best blog post ever!
[id] => 53dee8d26803fafe098b4569
)
)
)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.