1. Go to this page and download the library: Download adaiasmagdiel/rubik 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/ */
use AdaiasMagdiel\Rubik\Model;
class User extends Model
{
protected static string $table = 'users';
protected static function fields(): array
{
return [
'id' => self::Int(autoincrement: true, primaryKey: true),
'name' => self::Text(notNull: true),
'email' => self::Text(unique: true, notNull: true),
'created_at' => self::DateTime(default: 'CURRENT_TIMESTAMP')
];
}
}
User::createTable(ifNotExists: true);
// Create a new user
$user = new User();
$user->name = 'John Doe';
$user->email = '[email protected]';
$user->save();
// Retrieve a user
$user = User::find(1);
echo $user->name; // John Doe
// Update a user
$user->name = 'Jane Doe';
$user->save();
// Delete a user
$user->delete();
$user = new User();
$user->name = 'Alice Smith';
$user->email = '[email protected]';
$user->save(); // Inserts the record into the users table
// Find a user by ID
$user = User::find(1);
// Find all users
$users = User::all();
// Find a user by email
$user = User::findOneBy('email', '[email protected]');
$user = User::find(1);
$user->name = 'Alice Johnson';
$user->save(); // Updates the record
$user = User::find(1);
$user->delete(); // Deletes the record
// Select specific fields with conditions
$users = User::query()
->select(['name', 'email'])
->where('created_at', '2023-01-01', '>')
->orderBy('name', 'ASC')
->limit(10)
->all();
// Complex query with WHERE IN
$emails = ['[email protected]', '[email protected]'];
$users = User::query()
->whereIn('email', $emails)
->all();
// Paginate results
$results = User::query()
->paginate(page: 1, perPage: 20);
// Paginate results with where clause
$results = User::query()
->where('created_at', '2023-01-01', '>')
->paginate(page: 1, perPage: 20);
// Update multiple records
User::query()
->where('created_at', '2023-01-01', '<')
->update(['name' => 'Archived User']);
use AdaiasMagdiel\Rubik\Model;
use AdaiasMagdiel\Rubik\Relationship;
class Post extends Model
{
protected static string $table = 'posts';
protected static function fields(): array
{
return [
'id' => self::Int(autoincrement: true, primaryKey: true),
'user_id' => self::Int(notNull: true),
'title' => self::Text(notNull: true),
'content' => self::Text()
];
}
public function user(): Relationship
{
return $this->belongsTo(User::class, 'user_id');
}
}
class User extends Model
{
// ... fields() as defined earlier
public function posts(): Relationship
{
return $this->hasMany(Post::class, 'user_id');
}
}
// Create tables
User::createTable(ifNotExists: true);
Post::createTable(ifNotExists: true);
// Create a user and posts
$user = new User();
$user->name = 'Bob';
$user->email = '[email protected]';
$user->save();
$post1 = new Post();
$post1->user_id = $user->id;
$post1->title = 'First Post';
$post1->content = 'Hello, world!';
$post1->save();
$post2 = new Post();
$post2->user_id = $user->id;
$post2->title = 'Second Post';
$post2->content = 'Another post.';
$post2->save();
// Access relationships
$user = User::find(1);
$posts = $user->posts; // Array of Post instances
foreach ($posts as $post) {
echo $post->title . "\n";
}
$post = Post::find(1);
$user = $post->user; // User instance
echo $user->name; // Bob
class Comment extends Model
{
protected static string $table = 'comments';
protected static function fields(): array
{
return [
'id' => self::Int(autoincrement: true, primaryKey: true),
'post_id' => self::Int(notNull: true),
'content' => self::Text(notNull: true)
];
}
public function post(): Relationship
{
return $this->belongsTo(Post::class, 'post_id');
}
}
class Post extends Model
{
// ... fields() as defined earlier
public function comments(): Relationship
{
return $this->hasMany(Comment::class, 'post_id');
}
}
$post = Post::find(1);
$comments = $post->comments; // Array of Comment instances
$comment = Comment::find(1);
$post = $comment->post; // Post instance
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.