-
Notifications
You must be signed in to change notification settings - Fork 3
Symphony API ideas
Symphony 3 > Core API >
- Thoughts, examples: http://pastie.org/1817582
- Partial implementation: http://pastie.org/1818261
Getting an entry from a section, filtering by the name field (long)
$section = Sections::getById(1);
$entries = $section->getEntries();
$entry = $entries->getByName('You')->current();
Getting an entry from a section, filtering by tne name field (short)
$entry = Sections::getById(1)->getEntries()->getByName('Nick')->current();
Getting an entry from an entry id without knowing the section
$entry = Entries::getById(1);
Getting the name and body fields from an entry in section 1, filtering by the name field (long)
$section = Sections::getById(1);
$entries = $section->getEntries();
$entry = $entries->getByName('You');
$fields = $entry->get('name', 'body');
Getting the name field from an entry in a section, filtering by name. (short)
$field = Sections::getById(1)->getEntries()->getByName('You')->getName();
Getting the name field from entry 12
$field = Entries::getById(12)->getName();
Edit: I have given it some more thought, and I think a few things are not as optimal as they might be. Especially loading the fields will involve a lot of magic, which makes it hard to understand (when is the query really executed? How do I optimise my code?, etc).
So, instead of a magic function that gets a field from an entry (getName()
for instance), I think adding a filter function (telling the entry what fields to include) to the entry. Then those fields become simple properties of the entry object, that do not require database access every time they are accessed.
This function could be called loadFields(array $fields)
, by default all fields are loaded.
In the same line is also the getEntries()->getByName()
construct. I suggest to replace this by getEntriesByName()
.
-
setSectionId(int $section_id)
sets section ID -
setSectionHandle(string $section_handle)
sets section handle -
getFields()
returns a collection of field data -
getField(string $handle)
returns the data of a field -
setField(string $handle, mixed $value)
sets the data of a field -
save(int $section_id)
persist Entry in a given section -
save(string $section_handle)
persist Entry in a given section -
save(Section $section)
persist Entry in a given section -
asXML()
return the XML representation of an entry
-
get(int $id)
returns an Entry object matching the ID -
get(array $filters, array $options)
returns a collection of Entry objects matching filters, optional array of extra options (fields
,limit
,sort
,direction
)
-
get(int $id)
returns a Section object matching the ID -
get(string $handle)
returns a Section object matching the handle
Get an entry by ID (no section required), two ways:
$entry = Entries::get(123);
$entry = Entries::get(array('system:id' => 123))->current();
If the argument is numeric (ID) then a single Entry
is returned. If an array (of filters) then an iterator or collection of Entry
objects is returned.
Get an entry by title (section required)
$section = Sections::get(1);
$entry = $section->getEntries(array('title' => 'Lorem ipsum'))->current();
The getEntries()
method on a Section
object is the equivalent of Entries::get()
.
Get specific fields of an entry (e.g. data source included elements)
$section = Sections::get(1);
$entry = $section->getEntries(
array(
'title' => 'Lorem ipsum'
),
array(
'fields' => array('title', 'description', 'date'),
'limit' => 20,
'sort' => 'date',
'direction' => 'desc'
)
)->current();
Get the Title field from an entry
$entry = Entries::get(123);
$title = $entry->getField('title');
Create new Entry in section 123
$entry = new Entry();
$entry->setField('title, 'Lorem ipsum');
$entry->save(123);
or
$section = Sections::get(1);
$entry = new Entry();
$entry->setField('title, 'Lorem ipsum');
$entry->save($section);