Jump to main content Jump to doc navigation

xPDOObject::addMany()

Adds an object or collection of objects related to this class.

Syntax

API Docs: xPDOObject::addMany()

boolean addMany (
   mixed &$obj,
   [string $alias = '']
)

Example

Add golf clubs to a bag and save.

$bag = $xpdo->newObject('GolfBag');
$bag->set('name',"Chris's Bag");
$bag->set('color','blue');
$clubs = array();
for ($i=1;$i<10;$i++) {
   $club = $xpdo->newObject('GolfClub');
   $club->set('name',$i.' Iron');
   $clubs[] = $club;
}
$bag->addMany($clubs);
$bag->save(); // saves both the bag and all the clubs

Nested Calls You can nest one addMany() call inside another and thus create all your related data via a single save() operation.

Troubleshooting

Remember that this operation is intended to be called only for objects whose relationships are defined as cardinality="many".

See Also

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • Fabian Christen
  • Dannevang Digital
  • Sepia River Studios
  • Chris Fickling
  • CrewMark
  • deJaya
  • Lefthandmedia
  • Murray Wood
  • eydolan
  • Following Sea
  • Anton Tarasov
  • Stéphane Jäggi
  • Raffy
  • Snow Creative
  • A. Moreno
  • Nick Clark
  • JT Skaggs
  • Helen
  • YJ
  • krisznet
  • Richard
  • Yanni

Budget

$301 per month—let's make that $500!

Learn more