PHP code example of xruff / basedbmodel

1. Go to this page and download the library: Download xruff/basedbmodel 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/ */

    

xruff / basedbmodel example snippets



namespace MyModels;

use XRuff\App\Model\BaseDbModel;

class UsersRepository extends BaseDbModel
{
	// no implementation needed
}


namespace MyModels;

use Nette\Database\Context;
use XRuff\App\Model\BaseDbModel;

class MyAnyNameRepo extends BaseDbModel
{
	public function __construct(Context $db)
	{
		parent::__construct($db, 'my_db_able_name');
	}
}

use Nette;
use MyModels\UsersRepository;

class MyUsersPresenter extends Nette\Application\UI\Presenter
{
	/** @var UsersRepository $usersModel */
	public $usersModel;

	public function __construct(
		UsersRepository $usersModel
	) {
		$this->usersModel = $usersModel;
	}

	public function actionDefault()
	{
		// returns Nette\Database\Table\ActiveRow
		// with first user with name John in table users
		$this->usersModel->getOneBy(['name' => 'John']);

		// or
		// set name as Joe for user with id 5 in table users
		// and returns Nette\Database\Table\ActiveRow with updated values
		$this->usersModel->save(['id' => 5, 'name' => 'Joe']);

		// or add new user with name Jane
		// and returns Nette\Database\Table\ActiveRow with just added row
		$this->usersModel->save(['name' => 'Jane']);

		// or some another method inherited from BaseDbModel
	}
}