function insert_tags() { $tags = new \TagModel(); $newTags = array('CSS', 'HTML', 'JavaScript', 'Techniques', 'Typography', 'Inspiration', 'Business', 'User Experience', 'E-Commerce', 'Design Pattern'); foreach ($newTags as $record) { $tags->title = $record; $tags->save(); $tags->reset(); } $this->test->message($this->getTime() . " imported 10 Tags "); }
function run($db, $type) { $test = new \Test(); // clear existing data \AuthorModel::setdown(); \TagModel::setdown(); \NewsModel::setdown(); \ProfileModel::setdown(); // setup models \AuthorModel::setup(); \TagModel::setup(); \NewsModel::setup(); \ProfileModel::setup(); // setup Author /////////////////////////////////// $author_id = array(); $author = new \AuthorModel(); $ac = $author::resolveConfiguration(); $author_pk = is_int(strpos($type, 'sql')) ? $ac['primary'] : '_id'; $author->name = 'Johnny English'; $author->save(); $author_id[] = $author->_id; $author->reset(); $author->name = 'Ridley Scott'; $author->save(); $author_id[] = $author->_id; $author->reset(); $author->name = 'James T. Kirk'; $author->save(); $author_id[] = $author->_id; $author->reset(); $allauthors = $author->find()->castAll(); $allauthors = $this->getResult($allauthors); $test->expect(json_encode($allauthors) == '[{"name":"Johnny English"},{"name":"Ridley Scott"},{"name":"James T. Kirk"}]', $type . ': all AuthorModel items created'); // setup Tags /////////////////////////////////// $tag_id = array(); $tag = new \TagModel(); $tc = $tag::resolveConfiguration(); $tag_pk = is_int(strpos($type, 'sql')) ? $tc['primary'] : '_id'; $tag->title = 'Web Design'; $tag->save(); $tag_id[] = $tag->_id; $tag->reset(); $tag->title = 'Responsive'; $tag->save(); $tag_id[] = $tag->_id; $tag->reset(); $tag->title = 'Usability'; $tag->save(); $tag_id[] = $tag->_id; $tag->reset(); $allTags = $this->getResult($tag->find()); $test->expect(json_encode($allTags) == '[{"title":"Web Design"},{"title":"Responsive"},{"title":"Usability"}]', $type . ': all TagModel items created'); // setup News /////////////////////////////////// $news_id = array(); $news = new \NewsModel(); $nc = $news::resolveConfiguration(); $news_pk = is_int(strpos($type, 'sql')) ? $nc['primary'] : '_id'; $news->title = 'Responsive Images'; $news->text = 'Lorem Ipsun'; $news->save(); $news_id[] = $news->_id; $news->reset(); $news->title = 'CSS3 Showcase'; $news->text = 'News Text 2'; $news->save(); $news_id[] = $news->_id; $news->reset(); $news->title = 'Touchable Interfaces'; $news->text = 'Lorem Foo'; $news->save(); $news_id[] = $news->_id; $news->reset(); $allnews = $this->getResult($news->find(null, array('order' => 'title'))); $test->expect(count($allnews) == 3 && $allnews[0]['title'] == 'CSS3 Showcase' && $allnews[1]['title'] == 'Responsive Images' && $allnews[2]['title'] == 'Touchable Interfaces', $type . ': all NewsModel items created'); // belongs-to author relation /////////////////////////////////// $author->load(); $news->load(array($news_pk . ' = ?', $news_id[0])); $news->author = $author; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect($news->author->name == 'Johnny English', $type . ': belongs-to-one: author relation created'); $news->author = NULL; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect(empty($news->author), $type . ': belongs-to-one: author relation released'); $news->author = $author->_id; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect($news->author->name == 'Johnny English', $type . ': belongs-to-one: relation created by raw id'); // belongs-to-many tag relation /////////////////////////////////// $tag1 = new \TagModel(); $tag1->load(array($tag_pk . ' = ?', $tag_id[0])); $tag2 = new \TagModel(); $tag2->load(array($tag_pk . ' = ?', $tag_id[1])); $news->tags = array($tag1, $tag2); $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect($news->tags[0]->title == 'Web Design' && $news->tags[1]->title == 'Responsive', $type . ': belongs-to-many: relations created with array of mapper objects'); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[1])); $news->tags = array($tag_id[1], $tag_id[2]); $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[1])); $test->expect(count($news->tags) == 2 && $news->tags[0]->title == 'Responsive' && $news->tags[1]->title == 'Usability', $type . ': belongs-to-many: relations created with array of IDs'); $news->tags = null; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[1])); $test->expect(empty($news->tags), $type . ': belongs-to-many: relations released'); $tag->reset(); $news->load(array($news_pk . ' = ?', $news_id[1])); $news->tags = $tag->load(array($tag_pk . ' != ?', $tag_id[0])); $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[1])); $test->expect($news->tags[0]->title == 'Responsive' && $news->tags[1]->title == 'Usability', $type . ': belongs-to-many: relations created with hydrated mapper'); $news->reset(); $tag->reset(); $news->load(array($news_pk . ' = ?', $news_id[2])); $news->tags = $tag_id[0] . ';' . $tag_id[2]; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[2])); $test->expect($news->tags[0]->title == 'Web Design' && $news->tags[1]->title == 'Usability', $type . ': belongs-to-many: relations created with split-able string'); $test->expect(is_object($news->tags) && $news->tags instanceof \DB\CortexCollection, $type . ': belongs-to-many: result is collection'); // has-one relation /////////////////////////////////// $profile = new ProfileModel(); $pc = $profile::resolveConfiguration(); $profile_pk = is_int(strpos($type, 'sql')) ? $pc['primary'] : '_id'; $profile->message = 'Hello World'; $profile->author = $author->load(array($author_pk . ' = ?', $author_id[0])); $profile->save(); $profile_id = $profile->_id; $profile->reset(); $author->reset(); $author->load(array($author_pk . ' = ?', $author_id[0])); $profile->load(array($profile_pk . ' = ?', $profile_id)); $test->expect($author->profile->message == 'Hello World' && $profile->author->name == "Johnny English", $type . ': has-one: relation assigned'); $profile->reset(); $profile->message = 'I\'m feeling lucky'; $profile->image = 'lolcat.jpg'; $author->reset(); $author->load(array($author_pk . ' = ?', $author_id[1])); $author->profile = $profile; $author->save(); $profile->reset(); $author->reset(); $author->load(array($author_pk . ' = ?', $author_id[1])); $test->expect($author->profile->message == 'I\'m feeling lucky', $type . ': has-one: inverse relation'); // has-many relation /////////////////////////////////// $author->load(array($author_pk . ' = ?', $author_id[0])); $result = $this->getResult($author->news); $test->expect($result[0]['title'] == "Responsive Images" && $result[0]['tags'][0]['title'] == 'Web Design' && $result[0]['tags'][1]['title'] == 'Responsive', $type . ': has-many inverse relation'); // many to many relation /////////////////////////////////// $news->load(array($news_pk . ' = ?', $news_id[0])); $news->tags2 = array($tag_id[0], $tag_id[1]); $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect($news->tags2[0]['title'] == 'Web Design' && $news->tags2[1]['title'] == 'Responsive', $type . ': many-to-many relation created'); $test->expect(is_object($news->tags2) && $news->tags2 instanceof \DB\CortexCollection, $type . ': many-to-many: result is collection'); $tag3 = $tag->load(array($tag_pk . ' = ?', $tag_id[2])); $news->tags2[] = $tag3; $news->save(); $a = count($news->tags2); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect(count($news->tags2) == 3 && $a == 3, $type . ': many-to-many relation added implicitly'); $news->load(array($news_pk . ' = ?', $news_id[0])); $news->tags2 = NULL; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect(is_null($news->tags2), $type . ': many-to-many relation released'); $news->reset(); $news->title = 'Can it run Crysis?'; $news->text = 'XOXO'; $news->tags2 = array($tag_id[0]); $news->save(); $news_id[] = $news->_id; $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[3])); $a = count($news->tags2); $tag1 = $tag->find(array($tag_pk . ' = ?', $tag_id[0])); $b = count($tag1[0]->news); $c = $tag1[0]->news[0]->title == 'Can it run Crysis?'; $news->erase(); $tag1 = $tag->find(array($tag_pk . ' = ?', $tag_id[0])); $d = count($tag1[0]->news); $test->expect($a == 1 && $b == 1 && $c && $d == 0, $type . ': many-to-many relation cleaned by erase cascade'); $news->load(array($news_pk . ' = ?', $news_id[0])); $all = $news->find(); $test->expect($all[1]->tags2 === NULL && $all[2]->author === NULL, $type . ': empty relations are NULL'); $arr = $news->cast(); $test->expect(is_array($arr['tags']), $type . ': collection becomes array in casted model'); if ($type == 'mongo') { $test->expect(is_string($arr['_id']), $type . ': id becomes string in casted model'); } /////////////////////////////////// return $test->results(); }
public function actionUpStyle() { $connection = Yii::app()->db; $Cates = array(); $Tags = array(); $isSuc = false; $userId = $_REQUEST['id']; $picDesc = $_REQUEST['desc']; $category_name = $_REQUEST['category_name']; $tag_name = $_REQUEST['tag_name']; $categories = explode(",", $category_name); $tags = explode(",", $tag_name); //本地 //$root = YiiBase::getPathOfAlias('webroot').Yii::app()->getBaseUrl(); //服务器代码 $root = YiiBase::getPathOfAlias('webroot'); $folder = $root . '/images/images/styles/' . $userId . '/'; $desFilePath; $tmpFilePath; $relPath = Yii::app()->getBaseUrl() . '/images/images/styles/' . $userId . '/'; //echo ($folder); //exit(); $this->mkDirIfNotExist($folder); if ($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/png" || $_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/pjpeg") { if ($_FILES["file"]["error"] > 0) { $isSuc = false; } else { $tmpFilePath = $_FILES["file"]["tmp_name"]; /* $array = explode("/", $tmpFilePath); var_dump($array[ count($array) -1]); $tmpPath = $tmpPath.$array[ count($array) -1]; */ $name = $this->getUploadImageFileName($_FILES["file"]["name"]); $desFilePath = $folder . $name; $desThumbPath = $folder . 'thumb/' . $name; $relPath = $relPath . $name; if (file_exists($desFilePath)) { unlink($desFilePath); //echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($tmpFilePath, $desFilePath); //生成缩略图 $im = null; $imtp = null; if ($_FILES["file"]["type"] == "image/gif") { $im = imagecreatefromgif($desFilePath); $imtp = 'gif'; } else { if ($_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg") { $im = imagecreatefromjpeg($desFilePath); $imtp = 'jpg'; } else { if ($_FILES["file"]["type"] == "image/png") { $im = imagecreatefrompng($desFilePath); $imtp = 'png'; } } } $this->mkDirIfNotExist($folder . 'thumb/'); CThumb::resizeImage($im, 100, 100, $desThumbPath, $imtp); //------------生成缩略图 $isSuc = true; } } } else { $isSuc = false; } if ($isSuc) { $styleModel = new StyleModel(); $tagModel = new TagModel(); $categoryModel = new CategoryModel(); $styleModel->image = $relPath; $styleModel->user_id = $userId; $styleModel->create_time = time(); $styleModel->like_num = 0; $styleModel->recommand_val = 0; if (!$styleModel->save()) { echo "style saved fail!"; return false; } foreach ($categories as $cate_name) { //搜索 category 表中是否存在此次上送的 名字。 如果没有就插入记录。 $ret = CategoryModel::model()->find('name=:cate_name', array(':cate_name' => $cate_name)); if (!isset($ret)) { $categoryModel->name = $cate_name; $categoryModel->create_time = time(); if (!$categoryModel->save()) { echo "category zd fail!"; } } //找到名字对应的_id。以便做插入 rel 关系表的时候用 $sql = "select _id from tbl_category where name = :CateName"; $command = $connection->createCommand($sql); $tmp = $command->query(array(':CateName' => $cate_name))->readAll(); array_push($Cates, array('cate_id' => $tmp[0]["_id"])); } foreach ($tags as $tag_name) { //搜索 tag 表中是否存在此次上送的 名字。 如果没有就插入记录。 $ret = TagModel::model()->find('name=:tag_name', array(':tag_name' => $tag_name)); if (!isset($ret)) { $tagModel->name = $tag_name; $tagModel->create_time = time(); if (!$tagModel->save()) { echo "tag saved fail!"; } } //找到名字对应的_id。以便做插入 rel 关系表的时候用 $sql = "select _id from tbl_tag where name = :TagName"; $command = $connection->createCommand($sql); $tmp = $command->query(array(':TagName' => $tag_name))->readAll(); array_push($Tags, array('tag_id' => $tmp[0]["_id"])); } //插入 tbl_style_category_rel 表 ( 可优化。 改成批量插入 ) foreach ($Cates as $Cate) { $styleCateRelModel = new StyleCateRelModel(); $styleCateRelModel->style_id = $styleModel->_id; $styleCateRelModel->category_id = $Cate['cate_id']; $styleModel->create_time = time(); if (!$styleCateRelModel->save()) { echo "styleCateRel saved fail!"; } } //插入 tbl_style_tag_rel 表 ( 可优化。 改成批量插入 ) foreach ($Tags as $Tag) { $styleTagRelModel = new StyleTagRelModel(); $styleTagRelModel->style_id = $styleModel->_id; $styleTagRelModel->tag_id = $Tag['tag_id']; $styleModel->create_time = time(); if (!$styleTagRelModel->save()) { echo "styleTagRel saved fail!"; } } $host = 'http://' . Yii::app()->request->getServerName(); //本地调试 $styleModel->image = $host . $styleModel->image; //服务器 //$styleModel->image = $host.Yii::app()->getBaseUrl().$styleModel->image; echo json_encode(array('result' => 1, 'res' => array('id' => $styleModel->_id, 'user_id' => $styleModel->user_id, 'image' => $styleModel->image))); } else { echo json_encode(array('result' => 0)); } }
<?php $tag_name = $_GET['TagName']; $brand_name = $_GET['BrandName']; $style_id = $_GET['StyleId']; if (trim($tag_name) !== "") { $connection = Yii::app()->db; //搜索 tag 表中是否存在此次上送的 名字。 如果没有就插入记录。 $tagModel = new TagModel(); $ret = TagModel::model()->find('name=:tag_name', array(':tag_name' => $tag_name)); if (!isset($ret)) { $tagModel->name = $tag_name; $tagModel->create_time = time(); $tagModel->save(); } //找到名字对应的_id。以便做插入 rel 关系表的时候用 $sql = "select _id from tbl_tag where name = :TagName"; $command = $connection->createCommand($sql); $tag_id = $command->query(array(':TagName' => $tag_name))->readAll(); if (isset($tag_id)) { //插入 tbl_style_tag_rel 表 $styleTagRelModel = new StyleTagRelModel(); $styleTagRelModel->style_id = $style_id; $styleTagRelModel->tag_id = $tag_id[0]['_id']; $styleTagRelModel->tag_name = $brand_name; $styleTagRelModel->create_time = time(); $styleTagRelModel->save(); } } $sql = "select B.name as tag_name, A.tag_name as brand_name\n\t\t\tfrom tbl_style_tag_rel A\n\t\t\tinner join tbl_tag B on B._id = A.tag_id\n\t\t\twhere A.style_id = :style_id order by B.name asc"; $command = $connection->createCommand($sql);