Laravel (Eloquent) Table || Peer equivalent
Asked Answered
H

1

9

Propel uses Peer classes, and doctrine used Table classes as a way to manipulate respective objects and object properties, without having to pollute the actual object with static methods.

After cursory glance of the laravel (eloquent) docs, I didn't see anything that eloquent provides for the same Peer or Table like functionality. My question is, does laravel (or eloquent) provide a namespace for such classes, or do I just use Table and let autoloader take care of the rest?

// Example use of a table class in doctrine 1.2
$user = UserTable::getInstance()->findById(1);

-- Update 1 --

Layman example of how a doctrine table class may be used:

class UserTable
{
    public static function getInstance()
    {
        return Doctrine_Core::getTable('User');
    }

    public function linkFoo($userId, array $foos)
    {
        $user = $this->findById($userId);

        foreach ($foos as $foo) {

            $user->foo = $foo;

            $user->save();
        }
    }
}

// SomeController.php
executeSaveFoo()
{
    UserTable::getInstance()->linkFoo($this->getUser(), array('foo1', 'foo2'));
}

The purpose of the doctrine table class is to provide an api for actions against respective objects which should not be in the controller, in the above example the linkFoo class will link provided foos to the respective user object.

I feel the separation between objects and 'table' classes is important, as an object shouldn't know how to instantiate nor hydrate itself.

Hearthstone answered 26/5, 2015 at 18:8 Comment(6)
Can you explain what your example above accomplishes or what exactly it's doing?Puiia
With Eloquent you only have one class per model (and usually database table) but you don't have to "pollute" it with anything. Please read the documentation and/or clarify what your problem is.Lacewing
I didn't say pollute it with 'anything', I was specific about 'static' methods. Does it make sense to pass the object to itself per the above example?Hearthstone
In Laravel you would create a Command class to execute the logic inside linkFoo(), rather than having it in your Model class, and call it from your controller; passing the user and array to the Command class.Puiia
@aethergy: Nice, if you would be so kind to post an answer with a basic example, I can accept, and possibly help other old school ORM users.Hearthstone
Besides commands you could also create a repository or use general service classes. Laravel / Eloquent doesn't force you into one pattern.Lacewing
P
5

As mentioned earlier, there is more than one way to accomplish the task, but here's a quick example using Commands.

Controller

namespace App\Http\Controllers;

//...
use App\Http\Requests\UpdateUserRequest;
use App\Commands\UpdateUser;
use App\User;
//...

class UserController extends Controller {

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update(UpdateUserRequest $request, $id)
{
    //gather request data
    $data = $request->all();

    //retrieve user
    $user= User::findOrFail($id);

    //update user
    $updateUser = \Bus::dispatch(
                        new UpdateUser($user, $data)
                     );

    //check if update was successful
    if($updateUser)
    {
        //update successful
        return redirect('/route/here')->with('message', 'User updated successfully');
    }
    else
    {
        //else redirect back with error message
        return redirect()->back()->with('error', 'Error updating user');
    } 
}
}

The UpdateUserRequest class would handle validation.

Command

namespace App\Commands;

use App\Commands\Command;

use Illuminate\Contracts\Bus\SelfHandling;

class UpdateUser extends Command implements SelfHandling {

    protected $user, $data;

    /**
     * Create a new command instance.
     */
    public function __construct($user, $data)
    {
        $this->user= $user;
        $this->data = $data;
    }

    /**
     * Execute the command.
     */
    public function handle()
    {
        //assign first name
        $this->user->first_name = $this->data['first_name'];

        //assign last name
        $this->user->last_name = $this->data['last_name'];

        //assign email address
        $this->user->email = $this->data['email'];

        //update user
        if($this->user->update())
        {
            //updated successfully, return true
            return true;
        }
        else
        {
            //else return false
            return false;
        } 
    }

}
Puiia answered 28/5, 2015 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.