Using this bundle is almost equal to how you use the Eloquent ORM in Laravel.
You can use the Query Builder:
// src/Controller/PostController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use WouterJ\EloquentBundle\Facade\Db;
class PostController extends AbstractController
{
public function indexAction(): Response
{
$posts = Db::table('posts')->where('created_at', '>', '...')->get();
return $this->render('post/list.html.twig', [
'posts' => $posts,
]);
}
}
If you enabled the Eloquent ORM, you can also use the Eloquent models. You can enable Eloquent in the configuration:
# config/packages/eloquent.yaml
wouter_eloquent:
eloquent: ~
Then you can create a model:
$ php bin/console make:model Post
Which generates a file like this:
// src/Model/Post.php
namespace App\Model\Post;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
}
Now, you can persist, fetch, delete and update your models where you like:
// src/Controller/PostController.php
namespace App\Controller;
use AppBundle\Model\Post;
// ...
class PostController extends AbstractController
{
// ...
/** @Route("/{postId}", name="read_post")
public function readAction(int $postId)
{
$post = Post::find($postId);
return $this->render('post/single.html.twig', [
'post' => $post,
]);
}
}
You may prefer to use services instead of the magic Facades. The bundle provides two useful services:
wouterj_eloquent
- This is theCapsule
class, it can handle all core methods of theSchema
andDb
facades;wouterj_eloquent.database_manager
- This service is equal to theDb
facade.
« Installation • Migrations »