2011-04-13 33 views
12

我只是學習如何包裝我的頭圍繞SQL和PHP。我有4個表結構如下選擇聲明,將執行以下操作

+-----------+ +------------+ +---------+ +----------+ 
| Project | | Slide  | | Shape | | Points | 
+-----------+ +------------+ +---------+ +----------+ 
| id  | | id  | | id  | | id  | 
+-----------+ | project_id | | cont_id | | shape_id | 
       +------------+ +---------+ | x  | 
                | y  | 
                +----------+ 

正如你可以看到表由ID鏈接一路下跌至點意味着項目將包含許多包含多種形狀包含了一些幻燈片點。

我有一個SQL查詢

SELECT slide.`id`, shape.`id`, points.`x_point`, points.`y_point` 
FROM `project`, `slide`, `shape`, `points` 
WHERE 1 = slide.`project_id` 
    AND slide.`id` = shape.`slide_id` 
    AND shape.`id` = points.`shape_id` 

我想是藉此查詢看起來像這樣

[0] => stdClass Object 
    (
     [id] => 27 
     [x] => 177 
     [y] => 177 
    ) 

[1] => stdClass Object 
    (
     [id] => 27 
     [x] => 178 
     [y] => 423 
    ) 

[2] => stdClass Object 
    (
     [id] => 27 
     [x] => 178 
     [y] => 419 
    ) 

[3] => stdClass Object 
    (
     [id] => 27 
     [x] => 178 
     [y] => 413 
    ) 

[4] => stdClass Object 
    (
     [id] => 27 
     [x] => 181 
     [y] => 399 
    ) 

[5] => stdClass Object 
    (
     [id] => 27 
     [x] => 195 
     [y] => 387 
    ) 

[6] => stdClass Object 
    (
     [id] => 27 
     [x] => 210 
     [y] => 381 
    ) 

[7] => stdClass Object 
    (
     [id] => 27 
     [x] => 231 
     [y] => 372 
    ) 

[8] => stdClass Object 
    (
     [id] => 27 
     [x] => 255 
     [y] => 368 
    ) 

[9] => stdClass Object 
    (
     [id] => 27 
     [x] => 283 
     [y] => 368 
    ) 
... AND CONTINUED FOR A LONG TIME 

我想是廢話的這個人面獸心數組轉換成結果更類似於這個的東西

[9] => stdClass Object 
     (
      [id] => ID OF LIKE SHAPES 
      [x] => Array(ALL THE X POINTS) 
      [y] => ARRAY(ALL THE Y Points) 
     ) 

我不能爲我的生活弄清楚如何將th就是這樣一個數組。

如果不能用我設計的查詢來完成,那麼有更好的查詢。也許有,抓住要點,然後需要的是把它放到一個數組,其中點的...我想我只是有一個想法......


新信息,

所以我增加了一個答案對於這個問題,我不知道這是否是標準方法。爲了幫助其他答案,如果我的不是一個好的解決方案,我也會在這裏添加我的思考過程。

查看我的答案,以獲取更多信息。

另外ORM如何與我的算法比較?

+4

+1提供的信息和措辭。 :)我認爲「糟糕的垃圾數組」很好地描述了PHP和[My] SQL。 – 2011-04-13 00:31:59

+1

聽起來像你想使用ORM。 [Doctrine](http://www.doctrine-project.org/)做得非常好,雖然有涉及的開銷 – Phil 2011-04-13 00:40:06

+1

@Phil Brown繼續... – austinbv 2011-04-13 00:42:08

回答

3

使用像Doctrine的ORM,您只需將其建模像

/** 
* @Entity 
*/ 
class Project 
{ 
    /** 
    * @Id @GeneratedValue 
    * @Column(type="integer") 
    */ 
    private $id; 

    /** 
    * @OneToMany(targetEntity="Slide", mappedBy="project") 
    */ 
    private $slides; 

    public function __construct() 
    { 
     $this->slides = new \Doctrine\Common\Collections\ArrayCollection; 
    } 
} 

/** 
* @Entity 
*/ 
class Slide 
{ 
    /** 
    * @Id @GeneratedValue 
    * @Column(type="integer") 
    */ 
    private $id; 

    /** 
    * @ManyToOne(targetEntity="Project", inversedBy="slides") 
    * @JoinColumn(name="project_id", referencedColumnName="id") 
    */ 
    private $project; 

    /** 
    * @OneToMany(targetEntity="Shape", mappedBy="slide") 
    */ 
    private $shapes; 
} 

等等......

http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html#one-to-many-bidirectional

當然,還有設置和處理相當數量的所涉及的開銷,但隨着域模型變得越來越複雜,您將會欣賞ORM。

+0

由於你對這個問題的評論我一直閱讀和我的主機沒有安裝它,也沒有能力安裝它:( – austinbv 2011-04-13 01:04:46

+0

@zobgib要求是PHP 5.3。如果你有,可以安裝它 – Phil 2011-04-13 01:06:03

+0

啊,謝謝。我真的想要一個不同的解決方案因爲學習另一個軟件總是很痛苦,如果沒有其他解決方案, – austinbv 2011-04-13 01:10:39

0

我希望這幫助:

<?php 
$newStdClass['id'] = $stdClass[$i]['id']; 
for($i=0;$i<count($stdClass);$i++) 
{ 
    $newStdClass['x'][] = $stdClass[$i]['x']; 
    $newStdClass['y'][] = $stdClass[$i]['y']; 
} 
?> 

假設$ sttClass是因爲你說你廢話陣列:d。

1

所以我一直在努力這一段時間,我想出了自己的答案。我會喜歡輸入,因爲我認爲這可能是BAD的方式來做到這一點。

這是我的思考過程。一個查詢很好,但是如果我們逐漸構建結果數組會怎樣。我的意思是我們可以用設計的SELECT語句遍歷表來構建結果數組。

這裏是我評論它的代碼,因爲我很難用文字描述我的算法。

/* $cur_project is set above from an input value. Assume any int 
    The algoritim Traverses a series of results and puts them into the proper places in a usable array. 
    The algorithim has an query count of NumberOfSlides + 2(NumberOfSlides)+1 which seems really high 
    For real word application if querying the DB is as bad as everyone says. 
*/ 
// A blank array to build up 
$projectArray = Array(); 

// I just want to see how many queries this thing generates 
$queryCount = 0; 

// Query 1 - This query will get all slides in a project. 
$slide_id = $this->db->query('SELECT slide.`id` 
           FROM `slide` 
           WHERE slide.`project_id` = '.$cur_project); 
$queryCount++; 

//Now traverse the results to Query 1 
foreach ($slide_id->result() as $slide_id){ 
    // In the project array add an element with the key that is 
    // the slide_id for all slides in that project. Then for each 
    // key also create a new empty array at each added element 
    $projectArray[$slide_id->id] = Array(); 

    // Query 2 - grab all the shapes that match the current slide in the current project! 
    // This is where things get inefficient. 
    $shape_id = $this->db->query('SELECT shape.`id` 
            FROM `shape` 
            WHERE shape.`slide_id` = '.$slide_id->id 
           ); 
    $queryCount++; 

    // Traverse the results to Query 2 
    foreach ($shape_id->result() as $shape_id) { 
     // For every slide now create a key that matches the shape and fill that array with 
     // info I need such as an array of the points. 
     $projectArray[$slide_id->id][$shape_id->id] = Array(
              'x_points' => Array(), 
              'y_points' => Array() 
             ); 
     // Query 3 - Ask the DB for x/y points for the current shape. You can see how for slides with lots of shapes 
     $points = $this->db->query('SELECT points.`x_point`, points.`y_point` 
            FROM `points` 
            WHERE points.`shape_id` = '.$shape_id->id 
           ); 
     $queryCount++; 
     // Traverse the Query 3 results 
     foreach ($points->result() as $point) { 
      // Populate the final arrays with the points 
      $projectArray[$slide_id->id][$shape_id->id]['x_points'][] = $point->x_point; 
      $projectArray[$slide_id->id][$shape_id->id]['y_points'][] = $point->y_point; 

     } 
    } 
} 

以上返回一個數組,看起來像這樣

Array 
(
    [1] => Array 
     (
      [27] => Array 
       (
        [x_points] => Array 
         (
          [0] => 177 
          [1] => 178 
          [2] => 178 
          [3] => 178 
          [4] => 181 
... 

這可以解釋爲

Array 
(
    [SLIDE_ID] => Array 
     (
      [SHAPE_ID] => Array 
       (
        [x_points] => Array 
         (
          [0] => 177 
          [1] => 178 
          [2] => 178 
          [3] => 178 
          [4] => 181 
... 

我的這個解決方案是什麼問題,我在上面評論說。我想你可以通過數組搜索原始結果來複制這些結果,如答案中所列。儘管這似乎更糟糕。

請爲我的生活告訴我如何改善這個評論對我有幫助。

非常感謝。