1. Go to this page and download the library: Download harp-orm/harp 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/ */
harp-orm / harp example snippets
// Model Class
use Harp\Harp\AbstractModel;
class UserModel extends AbstractModel
{
public static function initialize($config)
{
$config
->addRel(new Rel\BelongsTo('address', $config, Address::getRepo()));
}
public $id;
public $name;
public $email;
public $addressId;
public function getAddress()
{
return $this->get('address');
}
public function setAddress(Address $address)
{
return $this->set('address', $address);
}
}
// Saving new model
$user = new UserModel(['name' => 'my name']);
UserRepo::save($user);
// Loading model
$loadedUser = UserRepo::find(1);
var_dump($loadedUser);
// Loading related model
$address = $loadedUser->getAddress();
var_dump($loadedUser->getAddress());
use Harp\Harp\AbstractModel;
class User extends AbstractModel
{
// Configure the "Repo" object for this model class
// This holds all the database-specific configs,
// as well as relations with other models
public static function initialize($config)
{
$config
// Configure relations
->addRel(new Rel\BelongsTo('address', $config, AddressRepo::get()));
// Configure validations
->addAssert(new Assert\Present('name'))
->addAssert(new Assert\Email('name'));
}
// Public properties persisted as columns in the table
public $id;
public $name;
public $email;
public $addressId;
}
$user = User::find(10);
$user->name = 'new name';
$address = new Address(['location' => 'home']);
$user->setAddress($address);
// This will save the user, the address and the link between them.
User::save($user);
$user2 = User::find(20);
$user2->delete();
// This will remove the deleted user from the database.
User::save($user2);
// Model File
use Harp\Harp\AbstractModel;
use Harp\Harp\Model\SoftDeleteTrait;
class Order extends AbstractModel
{
use SoftDeleteTrait;
public static function initialize($config)
{
// The trait has its own initialize method that you'll have to call
SoftDeleteTrait::initialize($config);
}
}
$order = Order::find(2);
$order->delete();
// This will issue an UPDATE instaead of a DELETE, marking this row as "deleted".
Order::save($order);
$order = Order::find(2);
// This will return true
echo $order->isVoid();
$update = User::insertAll()
->columns(['name', 'id'])
->select(
Profile::selectAll()
->clearColumns()
->column('name')
->column('id')
->where('name', 'LIKE', '%test')
);
// INSERT INTO User (name, id) SELECT name, id FROM Profile WHERE name LIKE '%test'
$update->execute();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.