Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Permanently Deleting Content


Go to End


7 Posts   1357 Views

Avatar
Bagzli

Community Member, 71 Posts

7 September 2014 at 4:51am

Edited: 07/09/2014 4:54am

Hello,

I have a Work page which displays Videos. So I have a class Work as such:

<?php
class Work extends Page {
    private static $has_many = array(
        'Videos' => 'Video'
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();   
        $config = GridFieldConfig_RelationEditor::create();
        // Set the names and data for our gridfield columns {Field (corresponds to Video page fields) => Displayed Title}
        $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
            'VideoTitle' => 'Video Title',
            'VideoURL' => "Video URL",
            'VideoDescription' => "Video Description",
            'Thumbnail' => 'Cover'
        )); 
        // Create a gridfield to hold the video relationship
        $videosField = new GridField(
            'Videos', //Corresponds to $has_many array
            'All Videos', // Displayed Title on the page
            $this->Videos(), //Corresponds to $has_many array
            $config
        );      
        // Create a tab named "Videos" and add our field to it
        $fields->addFieldToTab('Root.Videos', $videosField); 
        return $fields;
    }
}
class Work_Controller extends Page_Controller {
    public function PaginatedPages(){
        if($this->request->getVar('keyword')){
            $tag = $this->request->getVar('keyword');
        }
        else{
            $tag = 'a';
        }
        $paginatedItems = new PaginatedList($this->Videos()->filterAny(array('VideoTitle:PartialMatch' => $tag, 'VideoDescription:Partialmatch' => $tag))->sort('ID ASC'), $this->request);
        $paginatedItems->setPageLength(6);
        return $paginatedItems;
    }
}

and I have a class Video as such:

<?php
class Video extends DataObject {
    private static $db = array(
        'VideoTitle' => 'Varchar',
        'VideoURL' => 'Varchar',
        'VideoDescription' => 'Text'
    );
    private static $has_one = array(
        'Thumbnail' => 'Image',
        'Work' => 'Work'
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $imageField = new UploadField('Thumbnail');
        $fields->addFieldToTab('Root.Main', $imageField);
        return $fields;
    }
}

I have added about 9 Videos, then I have deleted 4 of them through the CMS. When I query to display the videos on the Work page I the remaining 4, however when I query for them on the Home Page I get all 9. So when I clicked the delete link it did not delete my 5 videos, it just somehow hid them from the current page, however they still seem to exist as part of my Video collection somewhere in the database.

The way I am querying on the Home page is as this:

return Video::get();

So my questions are as follow:

1. Why are these videos not deleted?
2. How should I be querying for these videos on the home page so that they don't show up?
3. Is there a way I can permanently delete them when I click remove button in the cms?

Avatar
Bagzli

Community Member, 71 Posts

7 September 2014 at 5:39am

From my own investigation I discovered that when I click the delete link, all it does is switches WorkID to 0. So when I query for Video() it returns me all the videos as they are not deleted.

As I will not know what the WorkID is, how can I query to make sure I get active videos only? Also how do I delete the other videos completely?

Avatar
martimiz

Forum Moderator, 1391 Posts

8 September 2014 at 8:38pm

Edited: 08/09/2014 8:39pm

To get all active video's - that is the ones that have a WorkID != 0, you should be able to use a filter like this:

return Video::get()->filter(array('WorkID:not' => 0)):

The GridFieldDeleteAction component, that is part of the Gridfield RecordEditor, by default only unlinks the relation to the page. To permanently remove the object, the component should have been constructed with true as a param:

$gridfield->addComponent(new GridFieldDeleteAction(true));

Btw: there doesn't seem to be a method to update the component, so you'd probably have to remove it from config and add it again...

Avatar
Bagzli

Community Member, 71 Posts

10 September 2014 at 11:02am

Edited: 10/09/2014 11:21am

Thank you.

The delete function though, once I added it, it created a button which looks exactly the same as the one that was there and then when I use it to delete, it doesn't delete again.

public function getCMSFields() {
        $fields = parent::getCMSFields();   
        $config = GridFieldConfig_RelationEditor::create();
        // Set the names and data for our gridfield columns {Field (corresponds to Video page fields) => Displayed Title}
        $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
            'VideoTitle' => 'Video Title',
            'VideoURL' => "Video URL",
            'VideoDescription' => "Video Description",
            'Thumbnail' => 'Cover'
        ));
        $config->addComponent(new GridFieldDeleteAction(true));
        // Create a gridfield to hold the video relationship
        $videosField = new GridField(
            'Videos', //Corresponds to $has_many array
            'All Videos', // Displayed Title on the page
            $this->Videos(), //Corresponds to $has_many array
            $config
        );

        // Create a tab named "Videos" and add our field to it
        $fields->addFieldToTab('Root.Videos', $videosField); 
        return $fields;
    }

Avatar
martimiz

Forum Moderator, 1391 Posts

10 September 2014 at 7:45pm

Oops, looks like I misread the informatin, sorry for that :( It seems to be be the other way around. The GridFieldDeleteAction constructor reads:

	/**
	 *
	 * @param boolean $removeRelation - true if removing the item from the list, but not deleting it
	 */
	public function __construct($removeRelation = false) {
		$this->removeRelation = $removeRelation;
	}

And the RelationEditor config is by default loading the component as 'true', thereby not deleting it, so I suppose it should be loaded 'false' to delete the relation...

Avatar
Bagzli

Community Member, 71 Posts

11 September 2014 at 1:49pm

Hey, thanks for the answer but I got a little confused, exactly where in my code would I be calling what?

Avatar
martimiz

Forum Moderator, 1391 Posts

16 September 2014 at 7:37am

Edited: 16/09/2014 6:08pm

The same place you did before. I'd probably remove component before adding it anew, but maybe that isn't necessary?