<![CDATA[ Add rows inside Object itself - My Forums]]> https://forums.modx.com/thread/?thread=104962 <![CDATA[Add rows inside Object itself]]> https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564390
I have a master Class "Event" that has a function that will add dates to a date table. Very simplified it looks like that:
class Event extends xPDOSimpleObject {
    public function addDate {
        $date = $this->modx->newObject('eventDate');
        $date->set('title', $title);
        $date->save();
    }
}


This works perfect and the addDate function adds the date to the table. Now i would like to move this function inside the eventDate Class. But this does not work anymore. Simplified it looks like that:
class eventDate extends xPDOSimpleObject {
    public function addDate {
        $this->set('title', $title);
        $this->save();
    }
}


I thought i could just add new dates like this. But this does not work. Is there a good example how to communicate via xPDO with the DB inside the Class/Object itself?
]]>
quadro Mar 12, 2019, 05:22 PM https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564390
<![CDATA[Re: Add rows inside Object itself]]> https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564450 quadro Mar 14, 2019, 04:15 PM https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564450 <![CDATA[Re: Add rows inside Object itself (Best Answer)]]> https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564430 this useful.]]> BobRay Mar 13, 2019, 06:53 PM https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564430 <![CDATA[Re: Add rows inside Object itself (Best Answer)]]> https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564429 And now i would like to call a function of the sgfevent.class.php
To do that, first you create an instance of the sgfEvent class. As it's an xPDO(Simple)Object, you'd typically either create a new object, or get an object for that. In your snippet, after you loaded your Signfy service class that calls $this->modx->addPackage, you'd do something like this:

$event = $modx->getObject('sgfEvent', ['id' => 10]);
$event->addEvent('foo bar');


Or, for a new object:
$event = $modx->newObject('sgfEvent');
$event->set('some_field', 'some value');
$event->addEvent('foo bar');


]]>
markh Mar 13, 2019, 06:48 PM https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564429
<![CDATA[Re: Add rows inside Object itself]]> https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564426
What i try to do is to separate all model relatet functions into their model class: Event-Functions inside the event model class, Team-Functions inside the team model class, etc. I probably don't load/include them correctly into the snippet - but i can't find any tutorial/docs on it. The doodle example always just handles a single class.

So my snippet looks like that:
$sgf = $modx->getService('signfy','Signfy',$modx->getOption('signfy.core_path',null,$modx->getOption('core_path').'components/signfy/').'model/signfy/',$scriptProperties);
if (!($sgf instanceof Signfy)) return 'Error';

$signfy = new Signfy($modx, array());
return $signfy->test('test');


My signfy.class.php looks like that:
<?php
class Signfy {

    public $modx;
    public $config = array();
    public function __construct(modX &$modx,array $config = array()) {
        $this->modx =& $modx;
        $basePath = $this->modx->getOption('signfy.core_path',$config,$this->modx->getOption('core_path').'components/signfy/');
        $assetsUrl = $this->modx->getOption('signfy.assets_url',$config,$this->modx->getOption('assets_url').'components/signfy/');
        $this->config = array_merge(array(
            'basePath' => $basePath,
            'corePath' => $basePath,
            'modelPath' => $basePath.'model/',
            'processorsPath' => $basePath.'processors/',
            'templatesPath' => $basePath.'templates/',
            'chunksPath' => $basePath.'elements/chunks/',
            'jsUrl' => $assetsUrl.'js/',
            'cssUrl' => $assetsUrl.'css/',
            'assetsUrl' => $assetsUrl,
            'connectorUrl' => $assetsUrl.'connector.php',
        ),$config);

        $this->modx->addPackage('signfy',$this->config['modelPath']);
    }


    public function test($title) {
        return $title;
    }

}


And now i would like to call a function of the sgfevent.class.php
<?php
class sgfEvent extends xPDOSimpleObject {


    public function addEvent($title) {
        return 'Works';
    }


}


I think separating all function into their model classes would be the correct pattern. But now i am pretty stuck here. Working in a single Class works good. But i don't get them separated.]]>
quadro Mar 13, 2019, 05:44 PM https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564426
<![CDATA[Re: Add rows inside Object itself]]> https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564401
The code you shared, once de-simplified and made valid again, would work like this:

$eventDate = $modx->newObject('eventDate');
$eventDate->addDate($title); // assuming $title is supposed to be passed in?


If you want to skip the newObject bit, you could use a static method to do something like this:

$eventDate = eventDate::addDate($modx, $title);

for which the addDate code would look something like:

(Note eventDate has to be loaded into memory for static methods to work; xPDO doesn't do that automatically and model classes aren't autoloaded. Personally I've added a list of classes with static methods/constants into the service constructor, calling loadClass for each, to make sure they're always available everywhere.)

public static function addDate(modX $modx, $title) {
   $inst = $modx->newObject('eventDate');
   $inst->set('title', $title);
   $inst->save();
   return $inst;
}
]]>
markh Mar 12, 2019, 08:53 PM https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564401
<![CDATA[Re: Add rows inside Object itself]]> https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564399


]]>
BobRay Mar 12, 2019, 07:30 PM https://forums.modx.com/thread/104962/add-rows-inside-object-itself#dis-post-564399