New model in new context not saving

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
jafo66
Posts: 25
Joined: 06 Mar 2024, 04:42

New model in new context not saving

Post by jafo66 » 25 Mar 2024, 01:54

I have been able to create a model in my extension. In my project which references my extension, I'm able to get model and query the database successfully.

What I'm not understanding is why I'm not able to save a record. There is no exception (it is in a try/catch just to be sure)

Code: Select all

        try {
            // see if seasons works
            $seasonManager = MShop::create($context, 'season');
            $search = $seasonManager->filter(true);
            $search->slice(0, 10000);
            $seaons = $seasonManager->search($search);
            Log::info('Seasons found: ' . json_encode($seaons));
            $newitem = $seasonManager->create([
                'name' => '2024 Season - March 3x3',
                'code' => '2024spring3x3',
                'status' => 1,
            ]);
            $newitems = $seasonManager->save($newitem);
            Log::info('Seasons created: ' . json_encode($newitems));
        }
        catch (\Exception $e) {
            Log::error($e->getMessage());
        }
Key Details:
  • Model is here (inside my extension): ./src/MShop/Season/Manager/Standard.php. It is the copy of the example in the docs

    Code: Select all

    namespace Aimeos\MShop\Season\Manager;
    
    class Standard
        extends \Aimeos\MShop\Common\Manager\Base
        implements \Aimeos\MShop\Common\Manager\Iface
    {
        public function getSaveAttributes() : array
        {
            return $this->createAttributes( [
                'name' => [
                    'type' => 'string',
                    'required' => true,
                    'unique' => false
                ],
                'code' => [
                    'type' => 'string',
                    'required' => true,
                    'unique' => true
                ],
                'status' => [
                    'type' => 'int',
                    'required' => true
                ],
            ] );
        }
    }
    
  • The table is mshop_season - which all seemed to work as expected
  • I have changed the logging to level 7, but I'm not seeing anything in the logs that would tell me what the problem is
Let me know if you have any thoughts as to why I'm able to query but not save?

User avatar
aimeos
Administrator
Posts: 7894
Joined: 01 Jan 1970, 00:00

Re: New model in new context not saving

Post by aimeos » 27 Mar 2024, 09:57

Passing data directly to the create() method doesn't set the internal modified state so save() skips sending the data to the database. Use fromArray() instead:

Code: Select all

            $data = [
                'name' => '2024 Season - March 3x3',
                'code' => '2024spring3x3',
                'status' => 1,
            ];
            $newitem = $seasonManager->create()->fromArray( $data );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply