Skip to content

Instantly share code, notes, and snippets.

@objectivehtml
Last active December 30, 2015 03:29
Show Gist options
  • Select an option

  • Save objectivehtml/7769661 to your computer and use it in GitHub Desktop.

Select an option

Save objectivehtml/7769661 to your computer and use it in GitHub Desktop.
<?php
/*
* An proposed spec for Channel Data v1.0
* Inspired by Eloquent ORM
*/
// Event is a channel. Basically you create a model per channel. Each model is just a class.
// The only thing that is required to be set in the class is the $channel property.
$entry = new Event();
// It auto-sets the url title using __set() magic methods.
$entry->title = 'This is a title';
// Set custom fields, or any other channel data like entry_date
$entry->your_custom_field = 'Some value';
// Saves the entry to the db;
$success = $entry->save(); // Returns FALSE if errors
if(count($entry->errors))
{
// The save failed. So you can access the $errors property.
}
else
{
// Now you have access to the entry_id. var_dump($entry->entry_id);exit();
}
// Set the title again
$entry->title = 'This is a title (edit)';
// Now save the existing entry vs. creating a new one
$success = $entry->save(); // Returns FALSE if errors
// And to delete the entry you just do...
$entry->delete();
// Instantiate existing entries and returns repsected models
$event = Event::find(1);
$event = Event::findByUrlTitle(1);
$event = Event::query()->future()->open();
$event = Event::query()->expired()->closed();
$event = Event::query()->future()->notClosed();
$event = Event::having('event_name', 'Some Event Name')->notClosed()->future();
$sql = $event->sql(); // Output SQL
$obj = $event->get(); // Get Query Object
$count = $obj->count(); // Total Results
$obj->each(function($i, $course) {
// Loop through entire result set
});
// Return a collection of instantiated models. This is currently
// using the channel data conditional parser. But would be better
// to follow Eloquent's approach vs. existing Channel Data approach.
$events = Event::where(array(
'entry_date >' => strtotime('First day of the week')
));
// Any collections return a response object similar to what CodeIgniter returns
// but again, following Eloquents approach and provind more helper methods and
// chainable conditions might be best. However, all this should be handed off
// to CodeIgniter's db() singleton to reuse all the db connectivity. Channel
// Data would just build the query and handle the results.
if($events->num_rows())
{
foreach($events->result() as $event)
{
$event->title = 'I am editing the title in a loop';
$event->save();
}
}
@bjornbjorn

Copy link
Copy Markdown

Looks great!

One suggestion I have is that $entry->save() returns FALSE on error. This is cleaner to check for than if(count($entry->errors) IMO.

And of course, all third party fields should be sensible classes as well ...

$event->my_matrix_field = a Matrix object .. or alternatively an array of objects.

Any File (or Assets) references should have all info available, ie. server location path, url location, filename, extension, etc. Shouldn't be necessary to write the code to parse {filedir_1}blabla.jpeg for each project.

Ah, the bliss.

@objectivehtml

Copy link
Copy Markdown
Author

Great feedback.

I have this working btw, and it works well. I am refactoring into a more presentable package. I honestly need help with some of the PHP 5.3+ features. Being in EE so long, I am still new to autoloading and name spacing.

I like your suggestion for having various classes for the different add-ons. Right now, this is using the channel entries api to handle the add/edit/delete actions. How would these classes work with that? Would you opt to not use the regular API? I like using the API simple because things like Postmaster already work using the entries API hooks.

@objectivehtml

Copy link
Copy Markdown
Author

I just checked the code. It already returns the the values of save() and update().

public function save($data = FALSE)
{
    if($data)
    {
        $this->fill($data);
    }

    if(!$this->exists)
    {
        return $this->_createRecord($data);
    }
    else
    {
        return $this->_updateRecord($data);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment