如何将所有父类别与它们的子类别链接放在一页上,结构如下:
在同一页面上是所有父类别;当您单击类别时,其新类别将显示在新页面上。
CategoryController:
public function actionIndex(){
return $this->render('index', [
'model' => $this->outTree($id)
]);
}
public function getAllArray(){
$all = Category::find()->all();
$array = array();
foreach($all as $v){
$array[$v->parentId][] = $v;
}
return $array;
}
public function outTree($parentId) {
$array = $this->getAllArray();
if(isset($array[$parentId])){
echo '<div>';
foreach($array[$parentId] as $v){
echo '<div>'.$v->name.'</div>';
echo '<div>'.$v->id.'</div>';
echo '<div>'.$v->parentId.'</div>';
$this->outTree($v->id);
}
echo '</div>';
}
}
protected function findModel($alias)
{
if (($model = Category::findOne(['alias' => $alias, 'active' => 1])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
父类别与子类别的关系
public function getParent()
{
return $this->hasOne(Category::className(), ['id' => 'parentId']);
}
现在输出进入控制器,如何在视图中做出这样的结论?目前,所有父类别及其子类别都显示在同一页面上。我需要在UrlManager中注册某些内容吗?
答案 0 :(得分:0)
您可以这样做:
您的控制器:
this.HasKey(i => new {i.IntegrationId, i.Id});
您查看页面(索引):
namespace NewSequentialId
{
public class SQLGuidUtil
{
static object synclock = new object();
static uint seq = 0;
static byte[] seed = Guid.NewGuid().ToByteArray();
public static Guid NewSequentialId()
{
uint nextVal;
byte[] buf;
lock (synclock)
{
nextVal = seq++;
buf = (byte[])seed.Clone();
if (nextVal == 0xFFFFFFFF)
{
seed = Guid.NewGuid().ToByteArray();
seq = 0;
}
}
var seqbytes = BitConverter.GetBytes(nextVal);
if (BitConverter.IsLittleEndian)
{
buf[0] = seqbytes[3];
buf[1] = seqbytes[2];
buf[2] = seqbytes[1];
buf[3] = seqbytes[0];
}
else
{
buf[0] = seqbytes[0];
buf[1] = seqbytes[1];
buf[2] = seqbytes[2];
buf[3] = seqbytes[3];
}
return new Guid(buf);
}
}
}
您的查看页面(子页面):
public function actionIndex($parentId = null){
$models = Category::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('index', [
'models' => $models
]);
}
public function actionGetChilds($parentId){
$models = Children::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('childs', [
'models' => $models
]);
}
注意:我认为您在“类别”中有另一个名为GetChildern的关系 模型,您可以在索引操作中使用它来按类别获取所有子类别 这个
foreach($models as $model): echo Html::a($model->name, Url::to(['index', 'parentId' => $model->parent_id])); endforeach;