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

Can i change primaryKey per query? #301

Open
liorphp opened this issue Oct 16, 2019 · 3 comments
Open

Can i change primaryKey per query? #301

liorphp opened this issue Oct 16, 2019 · 3 comments

Comments

@liorphp
Copy link

liorphp commented Oct 16, 2019

When i use this line:
$this->_db->from($this->getTable(), $id)->fetch()

$id is the value of "primaryKey" column, the default is 'id', by:
lichtner\fluentpdo\src\Structure.php line 22 (Structure constructor).

This constructor activate when i set the fluentpdo object.
Can i set diffrent primaryKey OR Structure object per query?

I see the method getStructure on Query.php
I search something like setStructure method.

Thanks.

@cbornhoft
Copy link
Member

Yes, you can supply your own structure and pass it to the constructor of Query!

use Envms\FluentPDO\{Query, Structure};

$myStructure = new Structure('customId', 'foreignId');
$fluent = new Query($pdo, $myStructure);

It also supports custom callables if you want to get a little more complex:

$callable = 
    function($prefix)
    {
        $len = strlen($prefix);
        return "{$prefix}_{$len}";
    };

$myStructure = new Structure($callable, 'foreignId');

In the example above, if your main table name was archive, your primary key would be returned as archive_7.

@liorphp
Copy link
Author

liorphp commented Oct 17, 2019

I know that, but it's global solution.
I have difference primaryKey each table.

I dont do it on new Query(), i want to do it on any query.

@cbornhoft
Copy link
Member

Can you provide an example of these different primary keys? With callbacks available, essentially any naming you have can be created dynamically, regardless if the structure is only provided once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants