Multi Table Update/Delete in AtK4 - php

How can one achieve multi table update/delete in atk4?

You can add related entities on the model level:
https://stackoverflow.com/a/7466839/204819
If this is not an option, you can always create custom handlers for your "edit" and "delete" button and perform actions using internal ORM or even on Model level.

You can use functions beforeInsert, afterInsert, beforeDelete, afterDelete and beforeUpdate, afterUpdate to carry out additional processing in the database. For example
using a unzipped install of ATK 4.1.3 in your webroot creates a folder called agiletoolkit which is referred to below as ATKHOME.
Create a simple table TASKTYPE in mysql with three fields (id, tasktype_desc and budget_code) and another table TASKTYPE_BUDGET with just id and budget_code.
Create two models for the tables in ATKHOME/lib/Model as follows (you may need to create the Model directory if it doesnt exist)
class Model_TaskType extends Model_Table {
public $entity_code='tasktype';
public $table_alias='ty';
function defineFields(){
parent::defineFields();
$this->newField('id')
->mandatory(true);;
$this->newField('tasktype_desc')
->mandatory(true);
$this->newField('budget_code')
->mandatory(true);
}
public function afterInsert($new_id){
$ttb=$this->add('Model_TaskTypeBudget');
$ttb->set('id',$new_id)
->set('budget_code',$this->get('budget_code'));
$ttb->insert();
return $this;
}
public function beforeUpdate(&$data){
$ttb=$this->add('Model_TaskTypeBudget')->loadData($data['id']);
$ttb->set('budget_code', $data['budget_code']);
$ttb->update();
return $this;
}
public function beforeDelete(&$data){
$ttb=$this->add('Model_TaskTypeBudget')->loadData($data['id']);
$ttb->delete();
return $this;
}
}
Note if you are using innoDB and have foreign keys, you have to do the insert and delete in the right order e.g. if there was a foreign key from TaskTypeBudget to TaskType on ID, then it should use beforeDelete and afterInsert to prevent constraint violations.
class Model_TaskTypeBudget extends Model_Table {
public $entity_code='tasktype_budget';
public $table_alias='tyb';
function defineFields(){
parent::defineFields();
$this->newField('id')
->mandatory(true);
$this->newField('budget_code')
->mandatory(true);
}
}
And a page in ATKHOME/page like this
class page_tasktype extends Page {
function init(){
parent::init();
$p=$this;
$tt=$this->add('Model_TaskType');
$crud=$p->add('CRUD');
$crud->setModel($tt, array('id','tasktype_desc', 'budget_code'));
if($crud->grid)
$crud->grid->addPaginator(10);
}
}
Note also be careful to include an opening < ? php tag before each class line but DO NOT include a closing ? > as this may cause errors in the Ajax.
In ATKHOME/config-default.php, modify the mysql connection username and password from root/root to the user and password of your mysql database.
$config['dsn']='mysql://atktest:atktest#localhost/atktest';
and modify ATKHOME/lib/Frontend.php to uncomment line 8 which allows all pages to connect to the database (you can also just add the $this->dbConnect(); line to the page.
class Frontend extends ApiFrontend {
function init(){
parent::init();
$this->dbConnect(); //uncommented
In the same Frontend.php, insert the following around line 50 to add a button to the default menu to add our new page.
->addMenuItem('CRUD Test', 'tasktype')
Now go to your webbrowser and enter http://localhost/agiletoolkit and on the first page, click CRUD Test. Adding rows will result in a row being added to TASKTYPE and a row with the same id and budget_code to TASKTYPE_BUDGET. Editing the budget_code will be reflected in both tables and deleting the row will delete it from both tables.
How neat and simple is that once you know the functions are provided by ATk4 ?

Related

Retrieve Parent Model Through Pivot Table Laravel

I'm currently struggling with retrieving data towards a parent model. I'll drop my database, classes, and things I've tried before.
I have 4 tables: sales_orders, products, work_orders, and product_sales_order (pivot table between sales_orders and products).
SalesOrder.php
class SalesOrder extends Model
{
public function products()
{
return $this->belongsToMany(Product::class)
->using(ProductSalesOrder::class)
->withPivot(['qty', 'price']);
}
}
ProductSalesOrder.php
class ProductSalesOrder extends Pivot
{
public function work_orders()
{
return $this->hasMany(WorkOrder::class);
}
public function getSubTotalAttribute()
{
return $this->qty* $this->price;
}
}
WorkOrder.php
class WorkOrder extends Model
{
public function product_sales_order()
{
return $this->belongsTo(ProductSalesOrder::class);
}
public function sales_order()
{
return $this->hasManyThrough(
ProductSalesOrder::class,
SalesOrder::class
);
}
}
So, what I want to retrieve sales order data from work order since both tables don't have direct relationship and have to go through pivot table and that is product sales order. I've tried hasOneThrough and hasManyThrough but it cast an error unknown column. I understand that error and not possible to use that eloquent function.
Is it possible to retrieve that sales order data using eloquent function from WorkOrder.php ?
You cannot achieve what you want using hasOneThrough as it goes from a table that has no ID related to the intermediate model.
In your example you are doing "the inverse" of hasOneThrough, as you are going from a model that has the ID of the intermediate model in itself, and the intermediate model has the ID of your final model. The documentation shows clearly that hasOneThrough is used exactly for the inverse.
So you still should be able to fix this, and use a normal relation as you have the sales_orders_id in your model SuratPerintahKerja, so you can use a normal relation like belongsTo to get just one SalesOrder and define it like this:
public function salesOrder()
{
return $this->belongsTo(SalesOrder::class, 'sale_orders_id');
}
If you want to get many SalesOrders (if that makes sense for your logic), then you should just run a simple query like:
public function salesOrders()
{
return $this->query()
->where('sale_orders_id', $this->sale_orders_id)
->get();
}
Have in mind that:
I have renamed your method from sales_order to salesOrder (follow camel case as that is the Laravel standard...).
I have renamed your method from sales_order to salesOrders for the second code as it will return more than 1, hence a collection, but the first one just works with one model at a time.
I see you use sale_orders_id, but it should be sales_order_id, have that in mind, because any relation will try to use sales_order_id instead of sale_orders_id, again, stick to the standards... (this is why the first code needs more parameters instead of just the model).
All pivot tables would still need to have id as primary and auto incremental, instead of having the id of each related model as primary... Because in SuratPerintahKerja you want to reference the pivot table ProdukSalesOrder but it has to use both produks_id (should have been produk_id singular) and sale_orders_id (should have been sales_order_id). So if you were able to use something like produk_sales_order_id, you could be able to have better references for relations.
You can see that I am using $this->query(), I am just doing this to only return a new query and not use anything it has as filters on itself. I you still want to use current filters (like where and stuff), remove ->query() and directly use the first where. If you also want to add ->where('produks_id', $this->produks_id) that is valid and doesn't matter the order. But if you do so, I am not sure if you would get just one result, so ->get() makes no sense, it should be ->first() and also the method's name should be salesOrder.
Sorry for this 6 tip/step, but super personal recommendation, always write code in English and do not write both languages at the same time like produks and sales orders, stick to one language, preferrably English as everyone will understand it out of the box. I had to translate some things so I can understand what is the purpose of each table.
If you have any questions or some of my code does not work, please tell me in the comments of this answer so I can help you work it out.
Edit:
After you have followed my steps and changed everything to English and modified the database, this is my new code:
First, edit ProductSalesOrder and add this method:
public function sales_order()
{
return $this->belongsTo(SalesOrder::class);
}
This will allow us to use relations of relations.
Then, have WorkOrder as my code:
public function sales_order()
{
return $this->query()->with('product_sales_order.sales_order')->first();
}
first should get you a ProductSalesOrder, but then you can access ->sales_order and that will be a model.
Remember that if any of this does not work, change all the names to camelCase instead of kebab_case.

Symfony Doctrine Prevent Particular Entity's Record Deletion

imagine I have some doctrine Entity and I can have some records of this entity in the database which I dont want to be deleted, but I want them to be visible.
In general I can have entities, for which I have default records, which must stay there - must not be deleted, but must be visible.
Or for example, I want to have special User account only for CRON operations. I want this account to be visible in list of users, but it must not be deleted - obviously.
I was searching and best what I get was SoftDeletable https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/softdeleteable.md It prevents fyzical/real deletion from DB, but also makes it unvisible on the Front of the app. It is good approach - make a column in the Entity's respective table column - 1/0 flag - which will mark what can not be deleted. I would also like it this way because it can be used as a Trait in multiple Entities. I think this would be good candidate for another extension in the above Atlantic18/DoctrineExtensions extension. If you think this is good idea (Doctrine filter) what is the best steps to do it?
The question is, is this the only way? Do you have a better solution? What is common way to solve this?
EDIT:
1. So, we know, that we need additional column in a database - it is easy to make a trait for it to make it reusable
But
2. To not have any additional code in each repository, how to accomplish the logic of "if column is tru, prevent delete" with help of Annotation? Like it is in SoftDeletable example above.
Thank you in advance.
You could do this down at the database level. Just create a table called for example protected_users with foreign key to users and set the key to ON DELETE RESTRICT. Create a record in this table for every user you don't want to delete. That way any attempt to delete the record will fail both in Doctrine as well as on db level (on any manual intervention in db). No edit to users entity itself is needed and it's protected even without Doctrine. Of course, you can make an entity for that protected_users table.
You can also create a method on User entity like isProtected() which will just check if related ProtectedUser entity exists.
You should have a look at the doctrine events with Symfony:
Step1: I create a ProtectedInterface interface with one method:
public function isDeletable(): boolean
Step2: I create a ProtectionTrait trait which create a new property. This isDeletable property is annotated with #ORM/Column. The trait implements the isDeletable(). It only is a getter.
If my entity could have some undeletable data, I update the class. My class will now implement my DeleteProtectedInterface and use my ProtectionTrait.
Step3: I create an exception which will be thrown each time someone try to delete an undeletable entity.
Step4: Here is the tips: I create a listener like the softdeletable. In this listener, I add a condition test when my entity implements the ProtectedInterface, I call the getter isDeleteable():
final class ProtectedDeletableSubscriber implements EventSubscriber
{
public function onFlush(OnFlushEventArgs $onFlushEventArgs): void
{
$entityManager = $onFlushEventArgs->getEntityManager();
$unitOfWork = $entityManager->getUnitOfWork();
foreach ($unitOfWork->getScheduledEntityDeletions() as $entity) {
if ($entity instanceof ProtectedInterface && !$entity->isDeletable()) {
throw new EntityNotDeletableException();
}
}
}
}
I think that this code could be optimized, because it is called each time I delete an entity. On my application, users don't delete a lot of data. If you use the SoftDeletable component, you should replace it by a mix between this one and the original one to avoid a lot of test. As example, you could do this:
final class ProtectedSoftDeletableSubscriber implements EventSubscriber
{
public function onFlush(OnFlushEventArgs $onFlushEventArgs): void
{
$entityManager = $onFlushEventArgs->getEntityManager();
$unitOfWork = $entityManager->getUnitOfWork();
foreach ($unitOfWork->getScheduledEntityDeletions() as $entity) {
if ($entity instanceof ProtectedInterface && !$entity->isDeletable()) {
throw new EntityNotDeletableException();
}
if (!$entity instance SoftDeletableInterface) {
return
}
//paste the code of the softdeletable subscriber
}
}
}
Well the best way to achieve this is to have one more column in the database for example boolean canBeDeleted and set it to true if the record must not be deleted. Then in the delete method in your repository you can check if the record that is passed to be deleted can be deleted and throw exception or handle the situation by other way. You can add this field to a trait and add it to any entity with just one line.
Soft delete is when you want to mark a record as deleted but you want it to stay in the database.

Create MySQL view by migration script in Laravel 4

I'm trying to create view in MySQL in Laravel by migration script. How can we create MySQL view by migration script in Laravel 4?
How about this? Haven't tested it, but I think it should work.
class CreateMyView extends Migration {
public function up()
{
DB::statement( 'CREATE VIEW myview AS SELECT [your select statement here]' );
}
public function down()
{
DB::statement( 'DROP VIEW myview' );
}
}
And then you can create a model to access it:
class MyView extends Eloquent {
protected $table = 'myview';
}
And then to access the view from elsewhere in your app you can query it like you would any other model, e.g.
MyView::all(); // returns all rows from your view
MyView::where( 'price', '>', '100.00' )->get(); // gets rows from your view matching criteria
Props go to the following which provided info on how to do this:
http://laravel.io/forum/05-29-2014-model-with-calculated-sql-field-doesnt-paginate
http://forumsarchive.laravel.io/viewtopic.php?pid=51692#p51692
CAVEAT
Be careful if later migrations modify the tables underlying your view. The reason is that per the documentation:
The view definition is “frozen” at creation time, so changes to the underlying tables afterward do not affect the view definition. For example, if a view is defined as SELECT * on a table, new columns added to the table later do not become part of the view.
Really, I guess you'd have to be careful of stuff like that for any migration, so maybe this is not such a big deal.

How to make models use defaults in Phalcon PHP Framework?

If a table has defaults on certain fields and NULL is not allowed, one would expect the insert script to use those defaults, as MariaDB/MySQL usually does. For example, if the table products has an AI field "id", a required field "name" and two required fields "active" and "featured" which both default to 1, then the query
INSERT INTO products (name) VALUES ('someName');
automatically inserts 1 as the value of active and featured. However, when using Phalcon's models like so:
$product = new Products();
$product->setName('someName');
$product->save();
returns validation errors saying "active" and "featured" are required.
Is there a flag I should provide during model generation in order for Phalcon tools to harvest and input the defaults into Model classes, or another way to make Phalcon automatically use defaults if found? Best approach would be just ignoring the fields that weren't set, I reckon. Can I make the models do that?
You can use a raw database value to avoid that, in specific inserts:
<?php
use Phalcon\Db\RawValue;
$product = new Products();
$product->setName('someName');
$product->setType(new RawValue('default')); //use default here
$product->save();
Or, general before create/update for specific fields:
use Phalcon\Db\RawValue;
class Products extends Phalcon\Mvc\Model
{
public function beforeValidationOnCreate()
{
$this->type = new RawValue('default');
}
}
Or ignore these fields in every SQL INSERT generated:
use Phalcon\Db\RawValue;
class Products extends Phalcon\Mvc\Model
{
public function initialize()
{
$this->skipAttributesOnCreate(array('type'));
}
}
Although I find twistedxtra's answer fascinating from the aspect that Phalcon contains this wicked method to read the column default, I believe from a architectural point of view this might be the wrong approach as you rely on your database to define the defaults of the properties of your model.
I would set the default value when declaring the property and keep the logic in the application layer. But that's just me.
Use Like below
The skipAttributesOnCreate will make sure Phalcon does not attempt to put a a value in that column. The database will apply the default value.
public function initialize()
{
$this->setSource('table_name');
$this->skipAttributesOnCreate(['name_of_column']);
}

Agile toolkit CRUD

I'm testing Agile Toolkit and i don't understand what i'm doing bad.
I've a table named 'families' which CRUD works fine.
Another table named 'subfamilies' with a relationship n to 1 with families and CRUD works fine too.
But here is my problem, in table 'articles' i've a relationship 1 to 1 with families and another 1 to 1 with subfamilies and CRUD works, i can add delete and modify but when add button is clicked, if i select a family on the form, subfamilies combobox shows me all subfamilies not only ones that are from that family.
How can i specify that if a family is selected the combobox from the add form only show subfamilies relatives to that family?
Code:
file ./lib/Model/Articulos.php:
class Model_Articulos extends Model_Table {
public $entity_code = 'articulos';
function init(){
parent::init();
$this->addField('name')->mandatory(true);
$this->addField('description')->mandatory(true)->type('text');
$this->addField('familias_id')->mandatory(true)->refModel('Model_Familias');
$this->addField('subfamilias_id')->refModel('Model_Subfamilias');
}
}
file ./page/articulos.php:
class page_articulos extends Page {
function init(){
parent::init();
$crud=$this->add('CRUD');
$crud->setModel('Articulos');
if($crud->grid){
$crud->grid->getColumn('name');
$crud->grid->getColumn('familias');
$crud->grid->getColumn('subfamilias');
}
}
}
And finally you can get the mysql workbench eer model here
Thank you in advance,
Serxoz.
Technically, when you define a model, there is a no sub-link between family and subfamily, so your CRUD wouldn't know about the dependency.
To do what you need, you must fully understand the following two examples:
http://demo.atk4.com/demo.html?t=20
http://demo.atk4.com/demo.html?t=22
Next, create a form for adding a new record for Articulous. You will need to use MVCForm but will have to add some additional code for the sub-family field. When you have it done, you'll need to convert your code into a separate class such as Form_Articulos inherited from MVCForm. Your code should go inside the "setModel" method of your form.
Next, extend CRUD like this:
class MyCrud extends CRUD {
public $form_class='Form_Articulos';
}
The new form will be used for editing and addition. One remaining thing you may need is to add model-level validation.
class Model_Articulous ... {
...
function beforeUpdate(&$data){
$family_id=$this->getRef('subfamilias_id')->get('familia_id');
if($family_id != $this->get('familias_id'))
throw $this->exception('Subfamily does not belong to selected family');
}
}

Categories