PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label entities. Show all posts
Showing posts with label entities. Show all posts

Tuesday, January 11, 2022

[FIXED] Symfony doctrine reverse engineering and Apiplatform

 January 11, 2022     api-platform.com, doctrine-orm, entities, symfony     No comments   

Issue

Is there a way to create Apiplatform enabled entities directly from database schemas?

I successfully create the entities using php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity , but now i have to add manually the reference to Apiplatform resources in 120 entities like here. Is there another way? Thanks in advance.


Solution

This feature isn’t supported out of the box (but it would be nice to add it). There is an open issue to add this feature to MakerBundle, but this hasn’t been implemented at time of writing.

However, you can easily achieve the same effect by using the "search and replace" feature of your IDE or by using sed: find every occurrences of @ORM\Entity and replace them with:

@ORM\Entity
@\ApiPlatform\Core\Annotation\ApiResource

You may want to run PHP CS Fixer after that to change the fully qualified class name by a use statement.



Answered By - Kévin Dunglas
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, January 5, 2022

[FIXED] Saving multiple Entities for one Customer

 January 05, 2022     cakephp, cakephp-3.0, entities     No comments   

Issue

I created a form which saves the Date and the Menu a Customer ordered. Thanks to this question: cakephp 3.x saving multiple entities - newEntities I made my own form work.

The Problem that I have now is that i need to pick for every Date and Menu a Customer. Is there way to choose the Customer only one time for all new Dates and Menus?

First my Code which is very similar to the answer i linked:

OrdersController.php

{   
        $order = $this->Orders->newEntity();
        if ($this->request->is('post')) {
            $orders = $this->Orders->newEntities($this->request->getData());

            debug($order);
            foreach ($orders as $order)
            {
                $this->Orders->save($order);
            }
                $this->Flash->success(__('The order has been saved.'));

                return $this->redirect(['action' => 'index']);

            $this->Flash->error(__('The order could not be saved. Please, try again.'));
        }
        $customers = $this->Orders->Customers->find('list', ['limit' => 200]);
        $menuItems = $this->Orders->MenuItems->find('list', ['limit' => 200]);
        $this->set(compact('order', 'customers', 'menuItems'));
    }

add.ctp

<fieldset>
        <legend><?= __('Neue Bestellung') ?></legend>
        <?php
            echo $this->Form->control('0.customer_id', array('label' => __('Kunde',true)));     
            echo $this->Form->control('0.date', array('label' => __('Datum',true), ['empty' => true]));
            echo $this->Form->control('0.menu_item_id', array('label' => __('Menü',true), 'empty' => true));

            echo $this->Form->control('1.customer_id', array('label' => __('Kunde',true)));    
            echo $this->Form->control('1.date', array('label' => __('Datum',true), ['empty' => true]));
            echo $this->Form->control('1.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
        ?>
    </fieldset>

I know that i need to change this

echo $this->Form->control('0.customer_id', array('label' => __('Kunde',true)));

echo $this->Form->control('1.customer_id', array('label' => __('Kunde',true)));  

but i dont know into what. I first thought if i use customer_id without a number

echo $this->Form->control('customer_id', array('label' => __('Kunde',true)));  

or like this

echo $this->Form->control('0.1.customer_id', array('label' => __('Kunde',true)));  

it would work but i doesn't. Propably because it doesnt know where to save it cause its missing the index number in the first example and in he second example I created a new Index? I am a beginner in coding so maybe im completly wrong with that but I think thats why it doesnt work.

If I do it like I explained above, it doesnt save a value into customer. How can i choose a Customer and save multiple Dates and Menus for him?

EDIT

Because I didn't explain it precisely I try again.

My Goal is to create a Form which should save multiple Dates and Menus for a Customer. Inside the Form I want to choose 1 Customer, which I get with the customer_id. After that I want to choose multiple Dates (date) and Menus (menu_item_id) for that chosen Customer. If I press submit I want to get multiple Orders for the Customer I choose.

Example: I choose a customer (John Sample), and now I pick for him the Date (01.01.2019) and the Menu 7. After that I pick the Date (02.02.2019) and the Menu 6. After that I pick the Date (03.02.2019) and the Menu 2.

If I save now, I want to see that the Customer I choose (John Sample) has ordered on the Dates 01.01.2019, 02.01.2019, 03.01.2019 and which menus he ordered for the Date.

Example:

Customer: John Sample | Date: 01.01.2019 | Menu: 7

Customer: John Sample | Date: 02.01.2019 | Menu: 6

Customer: John Sample | Date: 03.01.2019 | Menu: 2

The Problem that I have now is I don't know how to do that. I know that I could just pick a new Customer for every Date and Menu, like this:

<fieldset>
        <legend><?= __('Neue Bestellung') ?></legend>
        <?php
            echo $this->Form->control('0.customer_id', array('label' => __('Kunde',true)));     
            echo $this->Form->control('0.date', array('label' => __('Datum',true), ['empty' => true]));
            echo $this->Form->control('0.menu_item_id', array('label' => __('Menü',true), 'empty' => true));

            echo $this->Form->control('1.customer_id', array('label' => __('Kunde',true)));    
            echo $this->Form->control('1.date', array('label' => __('Datum',true), ['empty' => true]));
            echo $this->Form->control('1.menu_item_id', array('label' => __('Menü',true), 'empty' => true));

        ?>
    </fieldset>

but that is not what I want to do. Because at the End I want to save up to 7 Orders and i dont want to choose the customer every time.

This brings me back to my Question how can I save one Customer, I pick in the form, and save multiple Dates and Menus for him?

I hope this made my Question a little clearer.

EDIT 2

My Order Table:

OrdersTable.php

class OrdersTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('orders');
        $this->setDisplayField('id');
        $this->setPrimaryKey(['id']);

        $this->belongsTo('Customers', [
            'foreignKey' => 'customer_id',
        ]);
        $this->belongsTo('MenuItems', [
            'foreignKey' => 'menu_item_id',
        ]);
        $this->belongsTo('Bills', [
            'foreignKey' => 'bill_id',
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmptyString('id', 'create');

        $validator
            ->date('date')
            ->allowEmptyDate('date');

        $validator
            ->boolean('bestellt')
            ->allowEmptyString('bestellt', false);

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn(['customer_id'], 'Customers'));
        $rules->add($rules->existsIn(['menu_item_id'], 'MenuItems'));
        $rules->add($rules->existsIn(['bill_id'], 'Bills'));

        return $rules;
    }
}


Solution

I finally found my Solution!
add.ctp


    <?php
        /* Kunden auswählen */
            echo $this->Form->control('customer_id', array('label' => __('Kunde',true)));     
        /* Erster Tag */
            echo $this->Form->control('0.date', array('label' => __('Datum',true), ['empty' => true]));
            echo $this->Form->control('0.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
            echo $this->Form->control('0.customer_id',['type' => 'hidden', 'empty' => true]);  
            echo $this->Form->control('0.bestellt');
        /* Zweiter Tag */    
            echo $this->Form->control('1.date', array('label' => __('Datum',true), ['empty' => true]));
            echo $this->Form->control('1.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
            echo $this->Form->control('1.customer_id',['type' => 'hidden', 'empty' => true]);      
            echo $this->Form->control('1.bestellt');
        /* Dritter Tag */
            echo $this->Form->control('2.date', array('label' => __('Datum',true), ['empty' => true]));
            echo $this->Form->control('2.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
            echo $this->Form->control('2.customer_id',['type' => 'hidden', 'empty' => true]);  
            echo $this->Form->control('2.bestellt');
        /* Vierter Tag */    
            echo $this->Form->control('3.date', array('label' => __('Datum',true), ['empty' => true]));
            echo $this->Form->control('3.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
            echo $this->Form->control('3.customer_id',['type' => 'hidden', 'empty' => true]);  
            echo $this->Form->control('3.bestellt');
        /* Fünfter Tag */
            echo $this->Form->control('4.date', array('label' => __('Datum',true), ['empty' => true]));
            echo $this->Form->control('4.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
            echo $this->Form->control('4.customer_id',['type' => 'hidden', 'empty' => true]);  
            echo $this->Form->control('4.bestellt');
        /* Sechster Tag */    
            echo $this->Form->control('5.date', array('label' => __('Datum',true), ['empty' => true]));
            echo $this->Form->control('5.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
            echo $this->Form->control('5.customer_id',['type' => 'hidden', 'empty' => true]);          
            echo $this->Form->control('5.bestellt');
        /* Letzter Tag */
            echo $this->Form->control('6.date', array('label' => __('Datum',true), ['empty' => true]));
            echo $this->Form->control('6.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
            echo $this->Form->control('6.customer_id',['type' => 'hidden', 'empty' => true]);            
            echo $this->Form->control('6.bestellt');
        ?>

My Javascript

$('#customer-id').change(function() {
    $('#0-customer-id').val($(this).val());
    $('#1-customer-id').val($(this).val());
    $('#2-customer-id').val($(this).val());
    $('#3-customer-id').val($(this).val());
    $('#4-customer-id').val($(this).val());
    $('#5-customer-id').val($(this).val());
    $('#6-customer-id').val($(this).val());
});

Im now copying my customer_id into the hidden fields. That way im finally only need to choose my Customer one time and can create an Order for a Week.

Maybe this isnt the cleanest way to do it but it works!



Answered By - Heldojin
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing