检索cakephp中的url路径

时间:2013-11-07 18:20:02

标签: arrays database cakephp-2.0

我正在尝试检索图片的“网址”路径。我对变量$ media有一个问题,它不应该是空的

数据库表'Media'有此

id|name|url|posts_id|type

MediaController.php

function admin_show($id = null){
    if($id){
        $this->Media->id = $id;
        $media = current($this->Media->read());
        echo 'results for $media'."\n";
        debug($media);
        $d['url'] = Router::url('/img/'.$media['url']);//for the Absolute URL
        echo 'results for $d'."\n";
        debug($d);
    }
    //I added this line to check if there is any contents in the "Table"
    $results = $this->Media->find('all');
    echo 'results for $results'."\n";
    debug($results);

}

,输出如下:

results for $media
   \app\Controller\MediasController.php (line 47)
false

results for $d
   \app\Controller\MediasController.php (line 50)
  array(
'url' => '/grafikart2/img/'
  )

results for $results
   \app\Controller\MediasController.php (line 54)
array(
(int) 0 => array(
    'Media' => array(
        'id' => '8',
        'name' => 'media',
        'url' => '2013/11/1003236-10151796510844965-658856574-n.jpg',
        'posts_id' => '1',
        'type' => 'image'
    )
),

我想在我的控制器中检索$ media ['url']

1 个答案:

答案 0 :(得分:0)

好的!

我在我的控制器中做了这个

$resultats = current($this->Media->query("SELECT * FROM medias  where id = $id LIMIT 2;"));
$media = $resultats['medias']['url'];

输出

'2013/11/1003236-10151796510844965-658856574-n.jpg'