pyrocms/pyrocms — pull request #2339 by dshoreman (2013-01-31 07:28:35+00:00)
Title: Convert Keywords module to use Eloquent models

Most eloquent conversion is complete, all that's left is to replace the keywords_applied creation query in the library. I avoided converting spaces and PSR-related syntax so as not to piss anyone off when they try merging, so that's todo as well.

I'm in dire need of caffeine so I can't remember everything I've tried, though I'll post a couple methods (and their outcomes) below.

Early on I was getting MX_Controller showing Its face in a lot of the stack traces, and there were a couple where it said something like "trying to get property of non-object Admin::foo", despite `foo` being called on the model.

### Using DB::table()

```php
use Illuminate\Database\Connection as DB;

...

class Keyword_m extends Model
{
...

public static function createApplied($hash, $keyword_id)
{
return DB::table('keywords_applied')->insert(array(...));
}

...
}
```
![Stack trace](http://i.imgur.com/LBmNkiL.png)

### Temporarily overriding $this->table

```php
...
public static function createApplied($id, $hash)
{
$table = self::getTable();

self::setTable('keywords_applied');

$data = self::create(array(...));

self::setTable($table);
return $data;
}

...
```
![Stack trace](http://i.imgur.com/m9eKFVX.png)

The first method probably needs an Illuminate\Connection object setup ready prior to actually using it, which in turn would need PDO etc etc...
The temporary table override should work in theory. I _think_ `$this` is always an instance of CI regardless of what class it's in, causing confusion in composer packages. Don't quote me on it though. I just wish I could remember what I did to get the stack traces.

Anyway, one surefire way to get this working in the short term would be to add a separate model for the keywords_applied table, but that seems like major overkill for the sake of one query. I'd be interested to hear other people's views on this?