Last active
February 29, 2020 21:27
-
-
Save claretnnamocha/6fbc30d4c17ec1ae902ab2ef4694ee23 to your computer and use it in GitHub Desktop.
php-alpha-orm CRUD snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use ALphaORM\ALphaORM as DB; | |
# Create new Record | |
$product = DB::create('product'); | |
$product->name = 'Running shoes'; | |
$product->price = 5000; | |
DB::store($product); // Stores the data in the database | |
# Create new Record with Foreign key | |
$author = DB::create('author'); | |
$author->name = 'Chimamanda Adichie'; | |
$book = DB::create('book'); | |
$book->title = 'Purple Hibiscus'; | |
$book->author = $author; // Makes a foreign key reference in the database | |
DB::store($book); | |
# Retrieve only one row from table | |
$book = DB::find('book','id = :bid', [ 'bid' => 1 ]); | |
print("{$book->title} by {$book->author->name}"); | |
# Retrieve all records in a table | |
$books = DB::getAll('book'); | |
foreach ($books as $book) { | |
print("{$book->title} by {$book->author->name}"); | |
} | |
# Retrieve all records with filter | |
$books = DB::findAll('book','pub_year > :year', [ 'year' => 1999 ]); | |
foreach($books as $book){ | |
print("{$book->title} by {$book->author->name}"); | |
} | |
# Update existing record in database | |
$product = DB::find('product', 'id = :pid', [ 'pid' => 1 ]); | |
$product->price = 500; // Change colunm value | |
DB::store($product); // Updates the record | |
# Update existing record and foreign key reference in database | |
$book = DB::find('book','id = :bid', [ 'bid' => 1 ]); | |
$book->author->name = 'New author'; // Changing data of foreign key table | |
$book->pub_year= '2003'; | |
DB::store($book); // Updates the record | |
# Delete a single record from database | |
$book = DB::find('book','id = :bid', [ 'bid' => 1 ]); | |
DB::drop($book); | |
# Delete all records in a given table | |
DB::dropAll('book'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment