当前位置: 首页>>代码示例>>PHP>>正文


PHP DataObject::get_one方法代码示例

本文整理汇总了PHP中SilverStripe\ORM\DataObject::get_one方法的典型用法代码示例。如果您正苦于以下问题:PHP DataObject::get_one方法的具体用法?PHP DataObject::get_one怎么用?PHP DataObject::get_one使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SilverStripe\ORM\DataObject的用法示例。


在下文中一共展示了DataObject::get_one方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: testMultipleRowInsert

 public function testMultipleRowInsert()
 {
     $query = SQLInsert::create('"SQLInsertTestBase"');
     $query->addRow(array('"Title"' => 'First Object', '"Age"' => 10, '"Description"' => 'First the worst'));
     $query->addRow(array('"Title"' => 'Second object', '"Age"' => 12));
     $sql = $query->sql($parameters);
     // Only test this case if using the default query builder
     if (get_class(DB::get_conn()->getQueryBuilder()) === 'SilverStripe\\ORM\\Connect\\DBQueryBuilder') {
         $this->assertSQLEquals('INSERT INTO "SQLInsertTestBase" ("Title", "Age", "Description") VALUES (?, ?, ?), (?, ?, ?)', $sql);
     }
     $this->assertEquals(array('First Object', 10, 'First the worst', 'Second object', 12, null), $parameters);
     $query->execute();
     $this->assertEquals(2, DB::affected_rows());
     // Check inserted objects are correct
     $firstObject = DataObject::get_one('SQLInsertTestBase', array('"Title"' => 'First Object'), false);
     $this->assertNotEmpty($firstObject);
     $this->assertEquals($firstObject->Title, 'First Object');
     $this->assertEquals($firstObject->Age, 10);
     $this->assertEquals($firstObject->Description, 'First the worst');
     $secondObject = DataObject::get_one('SQLInsertTestBase', array('"Title"' => 'Second object'), false);
     $this->assertNotEmpty($secondObject);
     $this->assertEquals($secondObject->Title, 'Second object');
     $this->assertEquals($secondObject->Age, 12);
     $this->assertEmpty($secondObject->Description);
 }
开发者ID:jacobbuck,项目名称:silverstripe-framework,代码行数:25,代码来源:SQLInsertTest.php

示例2: processRecord

 public function processRecord($record, $columnMap, &$results, $preview = false)
 {
     // We match by 'Code', the ID property is confusing the importer
     if (isset($record['ID'])) {
         unset($record['ID']);
     }
     $objID = parent::processRecord($record, $columnMap, $results, $preview);
     $group = DataObject::get_by_id($this->objectClass, $objID);
     // set group hierarchies - we need to do this after all records
     // are imported to avoid missing "early" references to parents
     // which are imported later on in the CSV file.
     if (isset($record['ParentCode']) && $record['ParentCode']) {
         $parentGroup = DataObject::get_one('SilverStripe\\Security\\Group', array('"Group"."Code"' => $record['ParentCode']));
         if ($parentGroup) {
             $group->ParentID = $parentGroup->ID;
             $group->write();
         }
     }
     // set permission codes - these are all additive, meaning
     // existing permissions arent cleared.
     if (isset($record['PermissionCodes']) && $record['PermissionCodes']) {
         foreach (explode(',', $record['PermissionCodes']) as $code) {
             $p = DataObject::get_one('SilverStripe\\Security\\Permission', array('"Permission"."Code"' => $code, '"Permission"."GroupID"' => $group->ID));
             if (!$p) {
                 $p = new Permission(array('Code' => $code));
                 $p->write();
             }
             $group->Permissions()->add($p);
         }
     }
     return $objID;
 }
开发者ID:jacobbuck,项目名称:silverstripe-framework,代码行数:32,代码来源:GroupCsvBulkLoader.php

示例3: testBasicUpdate

 public function testBasicUpdate()
 {
     $query = SQLUpdate::create()->setTable('"SQLUpdateTestBase"')->assign('"Description"', 'Description 1a')->addWhere(array('"Title" = ?' => 'Object 1'));
     $sql = $query->sql($parameters);
     // Check SQL
     $this->assertSQLEquals('UPDATE "SQLUpdateTestBase" SET "Description" = ? WHERE ("Title" = ?)', $sql);
     $this->assertEquals(array('Description 1a', 'Object 1'), $parameters);
     // Check affected rows
     $query->execute();
     $this->assertEquals(1, DB::affected_rows());
     // Check item updated
     $item = DataObject::get_one('SQLUpdateTestBase', array('"Title"' => 'Object 1'));
     $this->assertEquals('Description 1a', $item->Description);
 }
开发者ID:SpiritLevel,项目名称:silverstripe-framework,代码行数:14,代码来源:SQLUpdateTest.php

示例4: testAddToCsvColumnGroupsByCode

 public function testAddToCsvColumnGroupsByCode()
 {
     $existinggroup = $this->objFromFixture('SilverStripe\\Security\\Group', 'existinggroup');
     $loader = new MemberCsvBulkLoader();
     $results = $loader->load($this->getCurrentRelativePath() . '/MemberCsvBulkLoaderTest_withGroups.csv');
     $newgroup = DataObject::get_one('SilverStripe\\Security\\Group', array('"Group"."Code"' => 'newgroup'));
     $this->assertEquals($newgroup->Title, 'newgroup');
     $created = $results->Created()->toArray();
     $this->assertEquals(1, count($created[0]->Groups()->column('ID')));
     $this->assertContains($existinggroup->ID, $created[0]->Groups()->column('ID'));
     $this->assertEquals(2, count($created[1]->Groups()->column('ID')));
     $this->assertContains($existinggroup->ID, $created[1]->Groups()->column('ID'));
     $this->assertContains($newgroup->ID, $created[1]->Groups()->column('ID'));
 }
开发者ID:SpiritLevel,项目名称:silverstripe-framework,代码行数:14,代码来源:MemberCsvBulkLoaderTest.php

示例5: testAddExtensionLoadsStatics

 /**
  * Test {@link Object::add_extension()} has loaded DataExtension statics correctly.
  */
 public function testAddExtensionLoadsStatics()
 {
     // Object::add_extension() will load DOD statics directly, so let's try adding a extension on the fly
     DataExtensionTest_Player::add_extension('DataExtensionTest_PlayerExtension');
     // Now that we've just added the extension, we need to rebuild the database
     $this->resetDBSchema(true);
     // Create a test record with extended fields, writing to the DB
     $player = new DataExtensionTest_Player();
     $player->setField('Name', 'Joe');
     $player->setField('DateBirth', '1990-5-10');
     $player->Address = '123 somewhere street';
     $player->write();
     unset($player);
     // Pull the record out of the DB and examine the extended fields
     $player = DataObject::get_one('DataExtensionTest_Player', array('"DataExtensionTest_Player"."Name"' => 'Joe'));
     $this->assertEquals($player->DateBirth, '1990-05-10');
     $this->assertEquals($player->Address, '123 somewhere street');
     $this->assertEquals($player->Status, 'Goalie');
 }
开发者ID:SpiritLevel,项目名称:silverstripe-framework,代码行数:22,代码来源:DataExtensionTest.php

示例6: getTeamByTitle

 public function getTeamByTitle($title)
 {
     return DataObject::get_one("CsvBulkLoaderTest_Team", array('"CsvBulkLoaderTest_Team"."Title"' => $title));
 }
开发者ID:jacobbuck,项目名称:silverstripe-framework,代码行数:4,代码来源:CsvBulkLoaderTest.php

示例7: getRecord

 public function getRecord()
 {
     if (empty($this->record)) {
         $this->record = DataObject::get_one('UploadFieldTest_Record', '"Title" = \'Record 1\'');
     }
     return $this->record;
 }
开发者ID:SpiritLevel,项目名称:silverstripe-framework,代码行数:7,代码来源:UploadFieldTest.php

示例8: requireDefaultRecords

 /**
  * Add default records to database. This function is called whenever the
  * database is built, after the database tables have all been created. Overload
  * this to add default records when the database is built, but make sure you
  * call parent::requireDefaultRecords().
  *
  * @uses DataExtension->requireDefaultRecords()
  */
 public function requireDefaultRecords()
 {
     $defaultRecords = $this->stat('default_records');
     if (!empty($defaultRecords)) {
         $hasData = DataObject::get_one($this->class);
         if (!$hasData) {
             $className = $this->class;
             foreach ($defaultRecords as $record) {
                 $obj = $this->model->{$className}->newObject($record);
                 $obj->write();
             }
             DB::alteration_message("Added default records to {$className} table", "created");
         }
     }
     // Let any extentions make their own database default data
     $this->extend('requireDefaultRecords', $dummy);
 }
开发者ID:SpiritLevel,项目名称:silverstripe-framework,代码行数:25,代码来源:DataObject.php

示例9: getParent

 /**
  * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing
  * is returned.
  *
  * @param string $filter
  * @return DataObject
  */
 public function getParent($filter = null)
 {
     $parentID = $this->owner->ParentID;
     if (empty($parentID)) {
         return null;
     }
     $idSQL = $this->owner->getSchema()->sqlColumnForField($this->owner, 'ID');
     return DataObject::get_one($this->owner->class, array(array($idSQL => $parentID), $filter));
 }
开发者ID:jacobbuck,项目名称:silverstripe-framework,代码行数:16,代码来源:Hierarchy.php

示例10: logInWithPermission

 /**
  * Create a member and group with the given permission code, and log in with it.
  * Returns the member ID.
  *
  * @param string|array $permCode Either a permission, or list of permissions
  * @return int Member ID
  */
 public function logInWithPermission($permCode = "ADMIN")
 {
     if (is_array($permCode)) {
         $permArray = $permCode;
         $permCode = implode('.', $permCode);
     } else {
         $permArray = array($permCode);
     }
     // Check cached member
     if (isset($this->cache_generatedMembers[$permCode])) {
         $member = $this->cache_generatedMembers[$permCode];
     } else {
         // Generate group with these permissions
         $group = Group::create();
         $group->Title = "{$permCode} group";
         $group->write();
         // Create each individual permission
         foreach ($permArray as $permArrayItem) {
             $permission = Permission::create();
             $permission->Code = $permArrayItem;
             $permission->write();
             $group->Permissions()->add($permission);
         }
         $member = DataObject::get_one('SilverStripe\\Security\\Member', array('"Member"."Email"' => "{$permCode}@example.org"));
         if (!$member) {
             $member = Member::create();
         }
         $member->FirstName = $permCode;
         $member->Surname = "User";
         $member->Email = "{$permCode}@example.org";
         $member->write();
         $group->Members()->add($member);
         $this->cache_generatedMembers[$permCode] = $member;
     }
     $member->logIn();
     return $member->ID;
 }
开发者ID:jacobbuck,项目名称:silverstripe-framework,代码行数:44,代码来源:SapphireTest.php

示例11: testSuccessfulLoginAttempts

 public function testSuccessfulLoginAttempts()
 {
     Security::config()->login_recording = true;
     /* SUCCESSFUL ATTEMPTS ARE LOGGED */
     $this->doTestLoginForm('testuser@example.com', '1nitialPassword');
     $attempt = DataObject::get_one('SilverStripe\\Security\\LoginAttempt', array('"LoginAttempt"."Email"' => 'testuser@example.com'));
     $member = DataObject::get_one('SilverStripe\\Security\\Member', array('"Member"."Email"' => 'testuser@example.com'));
     $this->assertTrue(is_object($attempt));
     $this->assertEquals($attempt->Status, 'Success');
     $this->assertEquals($attempt->Email, 'testuser@example.com');
     $this->assertEquals($attempt->Member()->toMap(), $member->toMap());
 }
开发者ID:SpiritLevel,项目名称:silverstripe-framework,代码行数:12,代码来源:SecurityTest.php

示例12: addToGroupByCode

 /**
  * Adds the member to a group. This will create the group if the given
  * group code does not return a valid group object.
  *
  * @param string $groupcode
  * @param string $title Title of the group
  */
 public function addToGroupByCode($groupcode, $title = "")
 {
     $group = DataObject::get_one('SilverStripe\\Security\\Group', array('"Group"."Code"' => $groupcode));
     if ($group) {
         $this->Groups()->add($group);
     } else {
         if (!$title) {
             $title = $groupcode;
         }
         $group = new Group();
         $group->Code = $groupcode;
         $group->Title = $title;
         $group->write();
         $this->Groups()->add($group);
     }
 }
开发者ID:jacobbuck,项目名称:silverstripe-framework,代码行数:23,代码来源:Member.php

示例13: testRemoveFromGroupByCode

 public function testRemoveFromGroupByCode()
 {
     $grouplessMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'grouplessmember');
     $memberlessGroup = $this->objFromFixture('SilverStripe\\Security\\Group', 'memberlessgroup');
     $this->assertFalse($grouplessMember->Groups()->exists());
     $this->assertFalse($memberlessGroup->Members()->exists());
     $grouplessMember->addToGroupByCode('memberless');
     $this->assertEquals($memberlessGroup->Members()->Count(), 1);
     $this->assertEquals($grouplessMember->Groups()->Count(), 1);
     $grouplessMember->addToGroupByCode('somegroupthatwouldneverexist', 'New Group');
     $this->assertEquals($grouplessMember->Groups()->Count(), 2);
     $group = DataObject::get_one('SilverStripe\\Security\\Group', "\"Code\" = 'somegroupthatwouldneverexist'");
     $this->assertNotNull($group);
     $this->assertEquals($group->Code, 'somegroupthatwouldneverexist');
     $this->assertEquals($group->Title, 'New Group');
     $grouplessMember->removeFromGroupByCode('memberless');
     $this->assertEquals($memberlessGroup->Members()->Count(), 0);
     $this->assertEquals($grouplessMember->Groups()->Count(), 1);
     $grouplessMember->removeFromGroupByCode('somegroupthatwouldneverexist');
     $this->assertEquals($grouplessMember->Groups()->Count(), 0);
 }
开发者ID:jacobbuck,项目名称:silverstripe-framework,代码行数:21,代码来源:MemberTest.php

示例14: testGetCaseInsensitive

 public function testGetCaseInsensitive()
 {
     // Test get_one() with bad case on the classname
     // Note: This will succeed only if the underlying DB server supports case-insensitive
     // table names (e.g. such as MySQL, but not SQLite3)
     if (!DB::get_conn() instanceof MySQLDatabase) {
         $this->markTestSkipped('MySQL only');
     }
     $subteam1 = DataObject::get_one('dataobjecttest_subteam', array('"DataObjectTest_Team"."Title"' => 'Subteam 1'), true);
     $this->assertNotEmpty($subteam1);
     $this->assertEquals($subteam1->Title, "Subteam 1");
 }
开发者ID:SpiritLevel,项目名称:silverstripe-framework,代码行数:12,代码来源:DataObjectTest.php

示例15: testRenameFolderAndCheckTheFile

 /**
  * Tests for the bug #5994 - if you don't execute get_by_id prior to the rename or move, it will fail.
  */
 public function testRenameFolderAndCheckTheFile()
 {
     // ID is prefixed in case Folder is subclassed by project/other module.
     $folder1 = DataObject::get_one('SilverStripe\\Assets\\Folder', array('"File"."ID"' => $this->idFromFixture('SilverStripe\\Assets\\Folder', 'folder1')));
     $folder1->Name = 'FileTest-folder1-changed';
     $folder1->write();
     // Check if the file in the folder moved along
     $file1 = DataObject::get_by_id('SilverStripe\\Assets\\File', $this->idFromFixture('SilverStripe\\Assets\\File', 'file1-folder1'), false);
     $this->assertFileExists(AssetStoreTest_SpyStore::getLocalPath($file1));
     $this->assertEquals($file1->Filename, 'FileTest-folder1-changed/File1.txt', 'The file DataObject path uses renamed folder');
     // File should be located in new folder
     $this->assertEquals(ASSETS_PATH . '/FolderTest/.protected/FileTest-folder1-changed/55b443b601/File1.txt', AssetStoreTest_SpyStore::getLocalPath($file1));
 }
开发者ID:SpiritLevel,项目名称:silverstripe-framework,代码行数:16,代码来源:FolderTest.php


注:本文中的SilverStripe\ORM\DataObject::get_one方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。