• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 Using the entity to insert new records

AndrewSimm

Active member
#1
I asked this question on the demo board, but the board was closed before I could get an answer I understand.

Below is my entity that i am trying to insert into. The form needs to be available for any registered user. I am trying to figure out the XF2 way of doing things. Using procedural PHP it isn't very difficult to do a prepared statement. I am just a little confused on what I need to put into my controller (I assume the same controller as the page) to insert new records into the database.

Can someone give me a quick sample on how I would do this?

PHP:
class Predictions extends \XF\Mvc\Entity\Entity
{
    public static function getStructure(Structure $structure)
    {
        $structure->table = 'player_predictions';
        $structure->shortName = 'XF:Predictions';
        $structure->primaryKey = 'prediction_id';
        $structure->columns = [
            'prediction_id' => ['type' => self::UINT, 'autoIncrement' => true, 'changeLog' => false],
            'user_id' => ['type' => self::UINT, 'maxLength' => 8, 'changeLog' => false],
            'player_id' => ['type' => self::UINT, 'maxLength' => 8, 'changeLog' => false],
            'man_college_id' => ['type' => self::UINT, 'maxLength' => 4, 'changeLog' => false],
            'confidence' => ['type' => self::STR, 'maxLength' => 64, 'default' => '', 'changeLog' => false],
            'date_added' => ['type' => self::STR, 'default' => '', 'changeLog' => false]
        ];
        $structure->getters = [
            'date' => true
        ];
        $structure->relations = [
            'Player' => [
                'entity' => 'CIS\Backend:Player',
                'type' => self::TO_ONE,
                'conditions' => 'player_id',
                'primary' => true
            ],
             'CollegeTeam' => [
                'entity' => 'CIS\Backend:CollegeTeam',
                'type' => self::TO_ONE,
                'conditions' => 'man_college_id',
                'primary' => true
            ],
            'User' => [
                'entity' => 'XF:User',
                'type' => self::TO_ONE,
                'conditions' => 'user_id',
                'primary' => true
            ]
        ];

        return $structure;
    }

    //Get date
    public function getDate()
    {
        $string = date_create($this->date_added);
        return date_format($string,"M d, Y");
    }

}
 
#2
I am a beginner with XF2 too, so I hope I am not wrong.

First, I think you should use Singular for your Entity: Prediction

Next, the shortname should be YourName\Predictions:Prediction

In your controller:
PHP:
$prediction = \XF::em()->create('YourName\Predictions:Prediction');
then:

Do validation and set the values on your $prediction object.

then:
PHP:
$prediction->save();
Your can learn a lot of the XF2 Classes and copy or use their methods. I looked at Thread and Post classes for my content type, but they are the most complex ones (as expected).
 

AndrewSimm

Active member
#3
I am a beginner with XF2 too, so I hope I am not wrong.

First, I think you should use Singular for your Entity: Prediction

Next, the shortname should be YourName\Predictions:Prediction

In your controller:
PHP:
$prediction = \XF::em()->create('YourName\Predictions:Prediction');
then:

Do validation and set the values on your $prediction object.

then:
PHP:
$prediction->save();
Your can learn a lot of the XF2 Classes and copy or use their methods. I looked at Thread and Post classes for my content type, but they are the most complex ones (as expected).
Thank you, that makes sense and works perfectly.