PHP code example of granadaorm / granada

1. Go to this page and download the library: Download granadaorm/granada library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

granadaorm / granada example snippets



use Granada\Model;

class User extends Model
{
    public function posts() {
        return $this->has_many('Post');
    }
}

class Post extends Model {}

// select
$user = User::q()->where('name', 'John')->find_one();

// modify
$user->first_name = 'Doe';
$user->save();

// select relationship
$posts = $user->posts()->find_many();
foreach ($posts as $post) {
    echo $post->content;
}



use Granada\ORM;

ORM::configure('mysql:host=localhost;dbname=my_database');
ORM::configure('username', 'database_user');
ORM::configure('password', 'top_secret');

$results = User::q()->with('avatar', 'posts')->find_many();

foreach($results as $result){
    echo $result->avatar->img;
    foreach($result->posts as $post){
        echo $post->title;
    }
}

$results = User::q()->find_many();
foreach($results as $result){
    echo $result->avatar->img;
}

foreach (User::q()->where('active', 1)->find_many_lazy() as $id => $user) {
    echo $user->name;
}

// 200 rows per query
foreach (User::q()->find_many_lazy(200) as $user) { ... }

foreach (Car::q()->with('parts')->find_many_lazy() as $car) {
    echo $car->parts[0]->name;
}

// Basic - equivalent to with('manufactor')
$car = Car::q()->with_manufactor()->find_one(1);

// Limit columns selected
$car = Car::q()->with_manufactor(fn($q) => $q->select('id, name'))->find_one(1);
// SELECT `id`, `name` FROM `manufactor` WHERE ...

// Add ordering (has_many - multiple related records)
$manufactor = Manufactor::q()->with_cars(fn($q) => $q->select('id, name')->order_by_desc('name'))->find_one(1);
// SELECT `id`, `name`, `manufactor_id` FROM `car` WHERE ... ORDER BY `name` DESC

$car = Car::q()->with_manufactor(fn($q) => $q->select('name'))->with('owner')->find_one(1);

$owner = Owner::q()->with_car(fn($q) => $q->select('id, manufactor_id')
    ->with_manufactor(fn($q) => $q->select('name'))
)->find_one(1);

$car = Car::q()->with_parts(fn($q) => $q->select('part.id, part.name'))->find_one(1);

// chained relationships with dot notation
$results = User::q()->with('posts.comments')->find_many();

// OR

// chained relationships use the "with" reserved word. (usefull if you want to pass arguments to the relationships)
$results = User::q()->with(array('posts'=>array('with'=>array('comments'))))->find_many();

// SELECT * FROM user
// SELECT * FROM post WHERE user_id IN (....)
// SELECT * FROM comments WHERE post_id IN (....)

foreach($results as $result){
    foreach($posts as $post){
        echo $post->title;
        foreach($post->comments as $comment){
        echo $comment->subject;
        }
    }
}

// you can use arguments (one or more) to call the models relationships
$results = User::q()->with(array('posts'=>array('arg1')))->find_many();
// will call the relationship defined in the user model with the argument "arg1"

class QueryModelName extends \Granada\Query {
    public function aname($argument1, $argument2): static
    {
        return $this->where('property', 'value')->limit(10);
    }
}

ModelName::q()->aname($argument1, $argument2)->...

// In the Model
protected function set_title($value)
{
    $this->alias = strtoupper($value);
    return $value;
}

// outside of the model
$content_instance->set('title', 'A title');

// works with multiple set too
$properties = array(
    'title'   => 'A title',
    'content' => 'Some content'
);
$content_instance->set($properties);

// try it with a direct assignement
$content_instance->title = 'A title';

$allowed = array('name', 'email', 'phone');
$enquiry = Enquiry::create(array_intersect_key($data, array_flip($allowed)));

// In the Model

// Work on defined
protected function get_path($value)
{
    return strtolower($value);
}

// and non-defined attributes.
protected function mising_testing()
{
    return 'whatever';
}
...

// outside of the model
echo $content_instance->path; // returns the lowercase path value of $content_instance
echo $content_instance->testing; // returns 'whatever' since we defined a missing_{attribute_name}

// In the Model
public static $resultSetClass = 'TreeResultSet';

// outside of the model
var_dump(Content::q()->find_many());

// echoes
object(TreeResultSet)[10]
    protected '_results' => array(...)
....

Content::q()->with('media')->find_many();