PGBI / cakephp3-soft-delete

83 stars 60 forks source link

[Enhancement] Adding the possibility to select 'onlyDeleted' like we do with 'withDeleted' #10

Closed flashios09 closed 9 years ago

flashios09 commented 9 years ago

Hi @davidyell , Thanks for this plugin :+1: , If we pass the 'withDeleted' option to the finder $Table->find('all', ['withDeleted']) we will have all the rows(like we disable the plugin) but what if we want only the deleted rows ! I modified your code like this:

public function triggerBeforeFind()
    {
        if (!$this->_beforeFindFired && $this->_type === 'select') {
            parent::triggerBeforeFind();

            $aliasedField = $this->repository()->aliasField($this->repository()->getSoftDeleteField());
            if (is_array($this->getOptions()) && in_array('onlyDeleted', $this->getOptions())) {
                $this->andWhere($aliasedField);
            }else if (!is_array($this->getOptions()) || !in_array('withDeleted', $this->getOptions())) {
                $this->andWhere($aliasedField . ' IS NULL');
            }

        }
    }

Now we can pass the option 'onlyDeleted' like we do with 'withDeleted' to the finder :)

davidyell commented 9 years ago

Sounds like a good idea, however I'd probably just use the withDeleted option and add a condition to the find, so I don't think this is needed in the plugin. If you want to submit a PR with tests I'm sure that @pgbi would probably merge it.

PGBI commented 9 years ago

Closing the issue as you can just do: $table->find('all', ['withDeleted'])->where(['deleted IS NOT NULL']);

flashios09 commented 9 years ago

@PGBI I know how to add a condition to get the deletedOnly, i just suggested an option like we do to add the deleted. By the way your condition doesn't work, you have just to write ->where(['deleted']) not ->where(['deleted IS NOT NULL'])