Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entities with only primary key not saved in database #321

Closed
zhukovra opened this issue Feb 8, 2022 · 1 comment
Closed

Entities with only primary key not saved in database #321

zhukovra opened this issue Feb 8, 2022 · 1 comment
Assignees
Labels
status:ready for adoption Feel free to implement this issue. type:bug Bug

Comments

@zhukovra
Copy link

zhukovra commented Feb 8, 2022

What happened?

Preparation:
composer require cycle/orm

Simple script for reproduce problem:

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use Cycle\Database;
use Cycle\Database\Config;
use Cycle\ORM;
use Cycle\Schema;

$dbal = new Database\DatabaseManager(
    new Config\DatabaseConfig([
        'default' => 'default',
        'databases' => [
            'default' => ['connection' => 'sqlite']
        ],
        'connections' => [
            'sqlite' => new Config\SQLiteDriverConfig(
                connection: new Config\SQLite\FileConnectionConfig(__DIR__ . '/test.db'),
            ),
        ]
    ])
);

$dbal->database()->execute('DROP TABLE IF EXISTS users');

$registry = new Schema\Registry($dbal);
$entity = new Schema\Definition\Entity();

$entity
    ->setRole('user')
    ->setClass(User::class);

$entity->getFields()
    // uncomment next line and run script again
    // ->set('text', (new Schema\Definition\Field())->setType('string')->setColumn('text'))
    ->set('id', (new Schema\Definition\Field())->setType('primary')->setColumn('id')->setPrimary(true));

$registry->register($entity);

$registry->linkTable($entity, 'default', 'users');


$schema = (new Schema\Compiler())->compile($registry, [
    new Schema\Generator\RenderTables(),
    new Schema\Generator\SyncTables(),
]);

$orm = new ORM\ORM(new ORM\Factory($dbal), new ORM\Schema($schema));
$em = new ORM\EntityManager($orm);

$em->persist(new User('1'))->run();
echo 'ROWS COUNT: ' . count($dbal->database('default')->query('SELECT * FROM users')->fetchAll());

class User
{
    public function __construct(
        public string $text = '',
    ) {
    }

}

Run script:

ROWS COUNT: 0

Enable 'text' mapping in entity (uncomment ->set() line) and run script again:

ROWS COUNT: 1

Version

ORM 2.0.0
PHP 8.1
@zhukovra zhukovra added status:to be verified Needs to be reproduced and validated. type:bug Bug labels Feb 8, 2022
@roxblnfk roxblnfk added status:ready for adoption Feel free to implement this issue. and removed status:to be verified Needs to be reproduced and validated. labels Jun 6, 2022
BelaRyc added a commit to BelaRyc/orm that referenced this issue Apr 16, 2023
@roxblnfk roxblnfk linked a pull request Jul 26, 2023 that will close this issue
roxblnfk pushed a commit that referenced this issue Feb 5, 2024
@roxblnfk
Copy link
Member

roxblnfk commented Feb 8, 2024

Fixed in 2.7.0

@roxblnfk roxblnfk closed this as completed Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:bug Bug
Projects
Status: Released
Development

Successfully merging a pull request may close this issue.

2 participants