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

the "unset field" feature #323

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

ziaratban
Copy link
Contributor

@ziaratban ziaratban commented Oct 30, 2020

Q A
Is bugfix? no
New feature? yes
Breaks BC? no
Tests pass? yes

In mongodb , we can unset a field only in a document unlike the SQL databases. so we need this feature in update of ActiveRecord.

Syntax
$document->Unset('field1','field2','field3','fieldN' ...);

Example

//users collection
{
    _id    : ObjectId('5f9a84109d2ff23d816e7983'),
    name   : 'N-A',
    family : 'F-A',
    email  : 'a@example.com'
}
{
    _id    : ObjectId('5de5320014bf173d3e838102'),
    name   : 'N-B',
    family : 'F-B',
    email  : 'b@example.com'
}
$doc1 = MyActiveRecordClass::FindOne('5f9a84109d2ff23d816e7983');
$doc1->name = 'N-A(edited)';
$doc1->Unset('email');
$doc1->Save();

$doc2 = MyActiveRecordClass::FindOne('5de5320014bf173d3e838102');
$doc2->Unset('name','email');
$doc2->Save();

the user's collection after saving

{
    _id : ObjectId('5f9a84109d2ff23d816e7983'),
    name : 'N-A(edited)',
    family : 'F-A',
}
{
    _id : ObjectId('5de5320014bf173d3e838102'),
    family : 'F-B',
}

So what is the difference between php unset() function and yii2 Unset() method ?

#php unset();
$doc1 = MyActiveRecordClass::FindOne('5f9a84109d2ff23d816e7983');
$doc1->name = 'N-A(edited)';
unset($doc1->email); #only sets email attribute to null
$doc1->Save(); #updates only name attribute
#yii2 ActiveRecord::Unset();
$doc1 = MyActiveRecordClass::FindOne('5f9a84109d2ff23d816e7983');
$doc1->name = 'N-A(edited)';
$doc1->Unset('email'); #sets email attribute to null
$doc1->Save(); #unsets email field and updates name field

unset method in insert mode

$doc1 = new MyActiveRecordClass;
$doc1->name = "...";
$doc1->family= "...";
$doc1->Unset('name'); #like php > unset($doc1->name);
$doc1->Save(); #only add the family field

@samdark
Copy link
Member

samdark commented Oct 30, 2020

@ziaratban I've moved CI to GitHub actions so there are no incidental failures anymore. Would you please merge master branch into all your pull requests?

@samdark samdark added the status:code review The pull request needs review. label Oct 30, 2020
@samdark samdark added this to the 3.1.0 milestone Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:code review The pull request needs review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants