kirkbushell / eloquence

A drop-in library for certain database functionality in Laravel, that allows for extra features that may never make it into the main project.
MIT License
543 stars 58 forks source link

Can't extends orginal Model class #20

Closed agence86 closed 9 years ago

agence86 commented 9 years ago

Hi,

when i follow your readme.md, il loaded with composer, i included the service provider in app.php but when i create a new model object it doesn't execute the uuid generation. By the way the closure is loaded in the core laravel Model object but it doesn't affect this class...

Even when i try to call

$user= new User(); $user->generateNewUuid(); it throw undefined method in user (and it try to search the method in query builder)

any idea?

Thanks in advance

Cedric LE ROY

kirkbushell commented 9 years ago

Can you post a code example? Check that it's actually including the traits as required.

Your model has to extend the Eloquence model. If it's not doing that - you won't see those methods.

agence86 commented 9 years ago

Do i have to include the trait in each Models?

User Model

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function addresses()
    {
        return $this->belongsToMany('YetismailApi\Models\Moderator');
    }
}

IN a controller

$user = new User();
var_dump($user->generateNewUuid());

throws undefined method generateNewUuid in Builder class

Thanks

kirkbushell commented 9 years ago

No, just extending the Eloquence model is enough (which you're not doing).

agence86 commented 9 years ago

so i need to use use Eloquence\Database\Model; insted of use Illuminate\Database\Eloquent\Model;

So i dont understand the bind effect in service provider. I would think it will include extended methods in the core Model class

kirkbushell commented 9 years ago

Correct.

agence86 commented 9 years ago

Right thank you i'll try this way.

agence86 commented 9 years ago

Last question btw what is the effect of the bind in service provider so?

kirkbushell commented 9 years ago

The reason for the service provider, is to re-bind the model to the Eloquence implementation. This is necessary for things like pivot tables and camel-casing, ensuring they also abide by the same rules :)

kirkbushell commented 9 years ago

Without it, you end up with snake_case fields in pivot models, but camelCase everywhere else.

agence86 commented 9 years ago

ho ok!

Thank you very much! Have a nice day

kirkbushell commented 9 years ago

You too :)